0

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?

  • you can always check when the application starts if there is a app.conf file or not if not create one and if yes continue – Saddam Abu Ghaida Feb 21 '14 at 09:06
  • You can use Registry. Or it can be saved in the fixed locations for all users. – Sinatr Feb 21 '14 at 09:07
  • 1
    you could put it as an embedded resource and then write it to disk. The initial config file I believe is generated automatically if not found but you can then, I guess, just use the embedded config file and compare/overwrite the existing one. This would be a better approach than a registry method as the user/application needs access to the registry which can be an issue at times due to user account permissions and the environment. – Ahmed ilyas Feb 21 '14 at 09:13

4 Answers4

1

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

Dexters
  • 2,419
  • 6
  • 37
  • 57
  • I have only Visual Studio Express 2010 so I realised this with clickone: http://www.c-sharpcorner.com/uploadfile/37db1d/deploying-wpf-application-with-clickonce-deployment-techniques/ Your tip toke me on the right way – LittleProgrammer Feb 21 '14 at 09:42
1

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.

Sumeshk
  • 1,980
  • 20
  • 33
0

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.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

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

woutervs
  • 1,500
  • 12
  • 28
  • On a move on the same computer they are preserved, If you have to move from one pc to another, then you could foresee a method to export/import your settings. Either way, the app.config is not designed to store user settings. – woutervs Feb 21 '14 at 09:34