Tutorial :C# Settings xml file location


I have a project that reads in a file based on a value in a C# Setting class. This value however changes from machine to machine and Id rather not ask the user the first time the program is run as its a corporate environment, instead Id like it to be set in the installer, but where is the file located? Is there an easier method?

This is a visual studio addin not a standalone program


From your post it appears you have a windows application? , you can store an initial value in the application config, you can make an installer in Visual Studio and write custom actions that can write values to the file on first install in you install project.


The configure file is the easiest way to do what you are asking, however it is NOT the best. In fact it is recommended Not to use .config files for such cases.

  • whenever users install an 'update', there is a risk of overwriting their existing changes.

  • some businesses might have policy restrictions on the .config files.

  • the user cannot easily move his settings from one PC to another.

From my own experience, using XML, MS-Access, Registry or text files to store user settings has proven more useful than using the .config files.


I believe you are talking about designer-generated settings (the .settings file)?

The exact path usually contains some sort of a Hash (check this link). I usually have my own settings class which I serialize to and from xml using XmlSerializer, which gives me more freedom (I think C# settings files don't allow you to add custom enums, for example, or they make it a bit harder to do it than simply adding them to the .settings file).

However, maybe there is no need to set values during installation? For example, you could add a FirstStartup setting (set to true initially), which you can read when your App is started for the first time, and then set it to false. That way you can set your default settings when you detect a "first startup".


You will certainly need some sort of custom action at the end of your installer. You haven't mentioned what technology you're using to deploy your application so I will refrain from giving any specific guidance.

I recommend setting the value in your App.config. This is an xml file which will be named MyApplication.exe.config in the same directory as your application. Add it to your installer if it is not there already. In your installer, add a new setting:

<configuration>    <appSettings>      <add key="MySetting" value="My Value"/>    </appSettings>  </configuration>  

In your code, retrieve the setting:

String setting = ConfigurationSettings.AppSettings["MySetting"];  

If this is a machine-wide setting, this installer is the right place to set this. If you do it on the first execution you will run into a host of problems with permissions on the files and directories if the first person to run the app is a user with limited permissions.


Registry or an INI file or a XML file whatever suits you best.


Best option would be registry. Istalling the application will require Admin access so writing to the registry during installation will not be an issue.

More over if some one accidently deletes the ini or settings file then the program might stop working.


You said that this is for a corporate environment. You can create an administrative template for group policy to set the default in the registry. Then, your program can just query that one registry value if the default hasn't already been set.


I personally would never use web.config or app.config. Just read your own xml file, have a look at my post on this: http://www.picnet.com.au/blogs/Guido/post/2009/09/10/XML-Settings-Files-No-more-webconfig.aspx




To answer the "Where is the file located" bit of the original question for those interested (I'm assuming that the "settings file" in question is the "Settings File" item available from the "Add New Item" dialogue).

In Win7 (and probably all the other ones) the settings file will generate under the path:


The folder structure from here on out depends on your AssemblyInfo and build information, the "AssemblyCompany" field (if populated) is used for the top-most folder, beneath this is a folder copying the application executable name, followed by the build and then finally the settings file (named "*.config").

In my case, the complete path is as follows:



My case is different, my project is a class library (dll) which have the app.config file. I decided to create 4 application settings variables (title, url, limit, rate). To create this, i right-click ont he project --> Properties --> Settings. this values you can retrieve in code using this command --> Settings.Default.title, ...etc

the problem is let say i instantiate 5 objects (same object for example MyProject.MyClass) from this library, i want the instance be able to have its own settings. i mean each of the instance may have their xml setting file. can this be done?

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »