0

I have following problem:

When I run the executable of my WinForms application (under Release or Debug - WindowsFormsApplication.exe) it works fine. Now I can choose a save file (a text file) for loading settings in the program or enter them my own and save them into a file. The path to the file is written into my appconfig (WindowsFormsApplication.EXE.config)

Configuration config = ConfigurationManager.OpenExeConfiguration
(System.Windows.Forms.Application.ExecutablePath);

            config.AppSettings.Settings.Remove("Path");
            config.AppSettings.Settings.Add("Path", openFile.FileName);

            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");

            savePath = ConfigurationManager.AppSettings["Path"];

When I start the application the next time it should read the path via

savePath = ConfigurationManager.AppSettings["Path"];

if (savePath != "" || savePath != String.Empty)
            saveFile.LoadUserSettings();

There in LoadUserSettings it starts reading the file:

 if (File.Exists(form1.savePath))
        {  
            using (StreamReader reader = new    StreamReader(form1.savePath))
            {
                reader.ReadLine();
                //reading text file
            }
        }

However, the application crashes immediately and can not be started anymore. If I delete path from the appconfig it works again. Does someone know what happens here? In Visual Studio this works fine.

Canox
  • 557
  • 1
  • 7
  • 20
  • This may help http://stackoverflow.com/questions/11149556/app-config-change-value – Seano666 Apr 15 '16 at 21:18
  • Does not help. I get changes in my appconfig. Like said, in Studio this works without problems. But when I do this in my executable it changes the appconfig (the path is written into it) and when I start the executable the next time it crashes immediately. – Canox Apr 15 '16 at 21:27
  • Are you sure your app is crashing when *reading* `Path` and not when it's trying to *use* that value after it has been read? More code would help. – Ian Kemp Apr 15 '16 at 21:41
  • Ok added some code. I don't know when this happens. The executable does not even really start. There is just the message that the executable does not work anymore and they are searching for the problem... When using the file after it has been loaded everything works fine but when loading it a second time at startup (from the path) it crashes. So it has to do something with the appconfig – Canox Apr 15 '16 at 21:49
  • Have you checked eventvwr? Also, make sure you are writing the app.config in utf-8 encoding – Octanic Apr 15 '16 at 22:09
  • Why do you think “the application crashes immediately”? Why do you think the code you have posted has anything to do with the problem? You get an exception message? You need to show us whatever let you to that conclusion. – Dour High Arch Apr 15 '16 at 22:16
  • It does not open. It's the error message that the application can not be run anymore and they are looking online for a solution - like in other apps that crash. I hope you know what I mean - I don't get the error message in english. I think code has something do to with it because this happens after the filepath is written into the appconfig file. When I delete it from there then the application starts. – Canox Apr 16 '16 at 15:54

0 Answers0