2

I've developed a program that utilizes Settings within my C# program. When I build the program, a separate XML Config file is created, that contains the settings of the program.

I've been able to merge other files, like the .dll's and manifest file I use for the program, into the executable. I've tried the suggested solution of putting it as a resource, but I get an error saying that it can't find the file. Secondly, the user of the program can change the settings, and I think that the Settings being implemented within the Resources isn't the best way to go. I've also set the Build Action to Embedded Resource and it still creates a separate XML Config file. How could I also merge the XML Config file with the executable?

John
  • 447
  • 2
  • 8
  • 20
  • 1
    Visual Studio will generate the config file for you convenience, but it's important to note that a default value of each Settings entry exists in the .exe itself. Try moving the .exe to a different location that does not have access to the config file, and observe that the default values will still be loaded. – Guillaume CR Apr 20 '17 at 19:29
  • @TimonPost This is not the same problem. – John Apr 20 '17 at 19:29
  • @GuillaumeCR The program will not start unless the `XML Config` file is in the same directory. – John Apr 20 '17 at 19:30
  • Really? That's surprising. What is the exception message that you get? – Guillaume CR Apr 20 '17 at 19:30
  • Is your issue more of accessibility? You don't want the user to edit the file outside the application etc? If that's the case you could simply encrypt the data within the file. – Felix Castor Apr 20 '17 at 19:36
  • @GuillaumeCR There is no message. The program just doesn't start; doesn't even show up in Task Manager. – John Apr 20 '17 at 19:36
  • @FelixCastor Is there a quick example that you know of? – John Apr 20 '17 at 19:37
  • 1
    You can use Visual Studio to launch the isolated executable in debug mode. That will help you figure out why it doesn't start. Normally, the default value of your settings exist inside the executable, and it's entirely possible to launch it without a .config file present. That is your requirement, yes? – Guillaume CR Apr 20 '17 at 19:37
  • @GuillaumeCR No, the program partly depends on the Settings. As Felix mentioned, I don't want the user of the program to be able to edit the Settings outside the program. – John Apr 20 '17 at 19:38
  • @John - I don't think settings is appropriate for your approach. There's a whole lot of infrastructure in settings that you're kinda abusing. You're better off having some kind of serialized object (as a resource) and using that. – Clay Apr 20 '17 at 19:41
  • Get rid of the app.config in your source tree, too...and it won't make the thing.exe.config – Clay Apr 20 '17 at 19:44
  • If you don't want the user to modify the values, the easiest solution is to not use Settings. Just hardcode the value. – Guillaume CR Apr 20 '17 at 19:52
  • @GuillaumeCR how would changes be kept if they were hard coded? Every time the user would open the application he/she would need to change the settings again. – Felix Castor Apr 20 '17 at 20:21
  • I understand better now. I think @FelixCastor 's solution is the right one. – Guillaume CR Apr 20 '17 at 20:25

1 Answers1

1

Encrypting sections of the config file is pretty easy. There are a couple providers. The simple code for this can be seen here. I usually use it to encrypt connection strings.

  var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

  var section = config.GetSection("connectionStrings");


section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");

 config.Save(ConfigurationSaveMode.Full);

Note: this code only takes effect at runtime. Every build will remove the encryption and once you release it and run it as expected (outside the debugger) it will encrypt it.

Link to MSN page

Felix Castor
  • 1,598
  • 1
  • 18
  • 39