Question

The user's input is saved in the configuration file of the application because at every restart the software needs to use this input:

  public void setAppSetting(string key, string value)
    {
        //Laden der AppSettings
        Configuration config = ConfigurationManager.
                                OpenExeConfiguration(
                                System.Reflection.Assembly.
                                GetExecutingAssembly().Location);

        if (config.AppSettings.Settings[key] != null)
        {
            config.AppSettings.Settings.Remove(key);
        }

        config.AppSettings.Settings.Add(key, value);
        config.Save(ConfigurationSaveMode.Modified);
    }

     public string getAppSetting(string key)
    {

        Configuration config = ConfigurationManager.
                                OpenExeConfiguration(
                                System.Reflection.Assembly.
                                GetExecutingAssembly().Location);

        return config.AppSettings.Settings[key].Value;
    }

Load of the app settings:

private void Start_Closed(object sender, EventArgs e)
    {

        setAppSetting("benutzerEingabe", textBoxPath.Text);
        setAppSetting("benutzerEingabe", userConfigurePath);
    }

So when the user decrompess the zip file where the exe file exists there is the content file without a content. So I want it.

 <?xml version="1.0" encoding="utf-8" ?>
 <configuration>
 </configuration>

After the first use of the app the config is filled with the key:

  <?xml version="1.0" encoding="utf-8" ?>
  <configuration>
 <appSettings>
    <add key="benutzerEingabe" />
 </appSettings>
 </configuration>

The problem for me is when the user copies the exe to an other place and start the first time this app there is a new config file. That's the problem. Is there any possibility that the config file is implement in the exe file so that this file isn't create itself. Or is there any other solution?

Was it helpful?

Solution

What you need is an Installer exe or msi package. You can do this by creating a msi project in visual studio. With this method instead of providing an exe file, you will me giving the users an MSI file. So when installing msi, you can bundle your exe in such a way that you will automatically place the config file. You can also make this installer to create folders etc on user machines. Very usefull.

http://www.codeproject.com/Articles/568476/Creating-an-MSI-Package-for-Csharp-Windows-Applica

http://support.microsoft.com/kb/307353

http://msdn.microsoft.com/en-us/library/cc766795.aspx

OTHER TIPS

Better to create a file in a common location, Each time check the existence of the file in the common location and if exist read/write the value otherwise create the file

you can either use an XML file or a ini file for data saving. so if u change the exe location it doesn't effect your execution.

you can use

Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

as your common folder.

You can load a config file by hand by using ConfigurationManager.OpenExeConfiguration

There is a good example over here: it preserves the settings over move.

You could offcourse always use the Settings in c# they are made for those things. http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top