2

I have a config file for my application in C#. It works like a charm. On Initializing my GUI I read my Values and on changing them I change them in the file and save them.

read:

txtCPort.Text = XTD_List_T001.Properties.Settings.Default.cport;

write:

XTD_List_T001.Properties.Settings.Default.cport = txtCPort.Text;
XTD_List_T001.Properties.Settings.Default.Save();

But now I build my solution and I have an exe and an exe.config file. In my program it still works fine but it does not write/read anything from the exe.config file in my folder. It's like there is some hidden file that stores the real values somewhere.

He still needs my config file though, because when I delete it he throws an error.

How can I Change the behaviour, so that he actually uses my exe.config file?

Edit 1: I tried to explain in the comments why my question ist not an exact duplicate. I also added some Code. And I just now realized that i need to be able to deploy default Settings so i might not get around having a exe.config that works as the file in %appdata% is local right?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • 2
    Possible duplicate of [Where are the Properties.Settings.Default stored?](https://stackoverflow.com/questions/982354/where-are-the-properties-settings-default-stored) (assuming you are referring to these settings). – Stefan Mar 23 '18 at 10:07
  • There are also machine wide settings: https://stackoverflow.com/questions/2325473/where-is-machine-config – Stefan Mar 23 '18 at 10:08
  • And when using IIS, you can override some config settings as well. – Stefan Mar 23 '18 at 10:09
  • I just found this question aswell and wanted to Update it @Stefan. The Problem is I still would want my file to work or to not need it at all. As I said when i delete it i get an error. In the question you refered to it says that I should not need it, and that this file would be autogenerated when first using the exe (or did i understand that wrong)? – T. Kogler Mar 23 '18 at 10:16
  • Can you show some code? And do you write to these settings? – Stefan Mar 23 '18 at 10:20
  • Btw.: here is some more info about the subject. The thing is: if you save your settings (in code), than the framework assumes that they are user bound, and hence, saves it to the user profile. Only the user settings part is saved. That why you'll need the original app.config because there is a lot of other stuff in there – Stefan Mar 23 '18 at 10:24
  • It seems that he uses the exe.config for the initial creation of the user.config file in the app data. So it's basicly an initial file?. This is not optimal for me as i think a working config file in the release folder would be cool but it works for now ... – T. Kogler Mar 23 '18 at 10:57

1 Answers1

0

If you want your setting to only use your app.exe.config and not be user-specific you need to change the setting scope from User to Application in the Visual Studio settings designer:

enter image description here

Or, if you need to edit the app.config XML manually, move the setting from the <userSettings> block into a/the <applicationSettings> block.

My brief summary of MSDN's description of the architecture:

  • Application settings are read from the local computer's machine.config file or the app.exe.config file.
  • Default values for user-scoped settings can be stored in the app.exe.config files.
  • User-scoped settings are stored in a writable user.config file in the LocalUserAppDataPath folder.
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108