app.config not loading when App is on StartUp - c#

I have a problem. If I run my application by clicking on it, it loads the saved app.config settings.
However, I need to have the Application run at Startup. Got this working too, but when it loads it does not load the saved settings - just the default ones.
The first time my Application loads with the default settings I require the user to login to there account through a REST API, grab some data and store it. I then set
Properties.Settings.Default.is_installed = true
but when the app runs in Startup it loads the default (false).
Has anyone experienced this? Any help would be appreciated!

If you want to persist changes to user settings between application sessions, call the Save method, as shown in the following code:
Properties.Settings.Default.is_installed = true;
Properties.Settings.Default.Save();
Here is MSDN Reference 1.
Settings that are application-scoped are read-only, and can only be
changed at design time or by altering the .config file in between
application sessions. Settings that are user-scoped, however, can be
written at run time just as you would change any property value. The
new value persists for the duration of the application session. You
can persist the changes to the settings between application sessions
by calling the Save method.
Here is MSDN Reference 2.
Saving User Settings at Run Time:
Application-scope settings are read only, and can only be changed at
design time or by altering the .exe.config file in
between application sessions. User-scope settings, however, can be
written at run time, just as you would change any property value. The
new value persists for the duration of the application session. You
can persist changes to user settings between application sessions by
calling the Settings.Save method. These settings are saved in the
User.config file.

Related

My application Settings get reset after changing product version

I have created an application using Settings from Project Properties. This setting has been used to store some boolean and string variables. I know the settings are user base. So, it can be stored separately for each windows user. I have also added a feature to update application from web server. But, recently i have notice that when i have change the product version then all the settings are get reset. It should not be. How to avoid resetting all setting on version change.
This question is not for only c#. this problem also occurred in vb.
You can try Settings.Upgrade() or ApplicationSettingsBase.GetPreviousVersion
Note: You must call only once this method.
Client Settings

Global variable at application level inside with out putting into Application_start

I want to have a global variable at application level, now the thing is that the variable must not be in side Application_Start since it will be sustainable to IISRESET for getting updated. Is there any way to maintain a global variable at application level which can be updated without IISRESET.
There are three options,
Web.Config - where you can store custom application specific settings
Going back to db and store the settings and load on application reset or initial load.
Store in ASP.NET Cache/Enterprise library cache.

add app settings in ConfigurationManager without saving

I am picking my application settings from database.
I want to add these setting to my application at runtime. I do not want to use config file because i do not want that anyone can see my settings.
if i use :
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
and then set the settings. the problem is that i have to save and refresh the settings which saves the configuration physically in file.
If i directly use ConfigurationManager.Appsettings["key"] it also requires to have key(i put them without value) in app settings in advance.
I just want to maintain my global settings in memory so that user or anyone can't see settings. What is the solution ?

Best way to persist settings when application exists

I have a Metro app where I need to save some data about the current "session" so that the next time the user launches my application, this session data may be restored. Some of the data is not meant for the user, but to aid in which data should be displayed right away, and which should be displayed at a later time if requested by the user.
I have been using LocalSettings for other things but have just noticed that these settings only appear to last for the lifetime of the application.
How should we be storing settings that need to be saved to the App's data folder?
You can achieve it by binding the data in to certain format , saving it in a file , retrieve it when the app launches.
Create a ApplicationDataContainer.
Initialize a StorageFile with the name you wish.
Serialize your "theme/settings" object using
DataContractSerializer.
Write the content to the StorageFile instance created.
On Application launch:
Deserialize the data.
Populate in the way you want.
You can rather use XML as mentioned by #Lütfullah Kus
you can store log to xml file like "lastform:frmSomething;lastwindow:somewindow..." and you can reload it when app start again.

Storing application settings in C#

What is the best practice to store application settings (such as user name and password, database location ...) in C# ?
Hint: I am new to .net and C#
Application Configuration Settings that are application wide (non-user specific) belong in either app.config (for Desktop apps) or web.config (for Web apps).
Encrypting sections of a web.config file is quite simple as outlined in this Super Simple Example.
If you need to store User specific settings (like application settings, etc.) or Application wide settings not related to application configuration you can use a Settings file as described here:
User Settings in C#
I'm not sure what version of .net/Visual Studio it was introduced in, but you can right click on your project, choose 'Add New Item' and select 'Settings File' from the "Add New Item" window. This provides your project with a (named by default) Settings.settings file that you can configure all the settings you want to expose in.
You can define settings that you create to be either Application or User which means you can use this single interface to control global and user settings. Once you've created a setting in the Settings.settings file using the editor that Visual Studio provides, you can access it in code like this:
// Get a Setting value
var valueOfSetting1 = Settings1.Default.Setting1;
// Modify and save a Setting value
Settings1.Default.Setting1 = "New Value";
Settings1.Default.Save();
First option is the registry. It is easy, but it is not too safe for passwords. Another option is using a file that you create. This too isn't safe, unless you want to implement cryption.
Next option is using the Application Settings. This is also quite simple, but there are a few catches. First, right click on your project and go to Properties. There, under the Settings tab, you can store variables to which you can access from your program by
string password = Properties.Settings.Default.Password
You can also change them the same way, but ONLY IF the scope is set the User. WHen the scope is application-wide, VS does not allow you to change these variables for some odd reason. To save changes, you must call Save() as follows:
Properties.Settings.Default.Save();
These are saved in the User Data folder under C:\Documents and Settings\'Current User'\Local Settings\Application Data\
Another option would be to include them in your database, but since you are also storing your database location, this might not work for you.
I think app.config (non web app) or web.config (web app).
These sorts of settings usually land in Application Configuration Files (web.config, app.config).
http://www.devasp.net/net/articles/display/679.html
If you are storing passwords, you might also need to encrypt the configuration section in question.
http://msdn.microsoft.com/en-us/library/53tyfkaw.aspx
http://msdn.microsoft.com/en-us/library/ff650304.aspx
Note if you use app.config, you will see it get renamed to ..config, depending on if your output produces a DLL or an EXE.
As with the above replies suggest, app.config or the web.config is the best place for app settings.
If you need a more robust way of xml style tags for database, server settings and the like, you can use the configurationSection and create custom sections.
http://www.codeproject.com/KB/cs/CustomConfigurationSectio.aspx
For database passwords, the way i do it is have an encrypted string in the xml tag value and decrypt then when reading them, that way you dont expose the passwords.
appsettings config file, ini file(nini), embeddable database(sqlite,berkelydb/etc..),whatever method you like, it depends on your application size/performance consideration and design.

Categories

Resources