tags:

views:

3646

answers:

9

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

A: 

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

Aamir
As far as I am aware, it IS an xml file saved in a system folder, but I am not sure where it is stored.
Tom J Nowell
A: 

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.

Shoban
deletion of the settings file is unlikely but in the event that the settings are borked irepairably, a dialog will popup asking the end user for the path, but since this is not a desirable UI case it is a failsafe and not a primary means of setup.
Tom J Nowell
+1  A: 

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.

abmv
there is no sane 'default', well there are 3 depending on where you are, how would I write custom actions on install?
Tom J Nowell
http://www.codeproject.com/KB/install/SetupAndDeployment.aspx
abmv
A: 

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".

Groo
It is indeed that sort of settings, however I have a 'first startup' kind of dialog already, however the most likely person to run into this dialog is the end user, and not the person setting it up, hence the whole point of this question.
Tom J Nowell
Got it. I didn't realize that admin interaction is needed also. In that case you can maybe try to implement a custom SettingsProvider.But I remember I already had these problems and decided it would be easier to make a simple singleton class with Load/Save methods.
Groo
A: 

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.

Peter Tate
But this value is not static, and the necessary value changes on a per network basis,and there is more than oen network to be deployed on.
Tom J Nowell
http://www.codeproject.com/KB/install/SetupAndDeployment.aspx
Tom J Nowell
A: 

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?

A: 

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.

Sesh
A: 

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.

Ryan
A: 

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

Thanks

Guido

gatapia