1

I am tired of googling about how to use user.config instead of app.config in a managed C++ application. All I found was about C# and I can't figurate out how to translate to C++ (Properties namespace does not exist)

Anybody can put me in the way to learn about that? I need to know how to create, read and write a user.config file.

Thank you

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Juanma Lozano
  • 57
  • 1
  • 6
  • So what have you found so far...? – t3chb0t Dec 06 '14 at 06:30
  • I have found that in C# projects, VS creates a static class called Default no read/write setting that will be stored in user.config. I have found that this class is inside Properties which is a namespace only present in C#. I have found thought http://stackoverflow.com/questions/2632806/how-to-write-to-a-user-config-file-through-configurationmanager (in C#) that you can use ConfigurationUserLevel with ConfigurationManager to access to user.config, but no section is accessible inside it (appSettings throw blocked error, default is null) – Juanma Lozano Dec 06 '14 at 12:25
  • Also, UserSettings doesn't exist in C++ and MSDN doesn't help in C++... All code I found is relevant with C#. I began working with app.config (successfuly) but I don't want to need admin rights to write on it (because it is Program Files folder). So, I prefer to use user.config which is concerned to the user and not to the whole system. – Juanma Lozano Dec 06 '14 at 12:36
  • You shouldn't change the machine app.config but create an app.cofing for your application. For that you don't need admin rights. Look at the demo project I attached to the answer. – t3chb0t Dec 06 '14 at 13:02
  • The C++ IDE is missing the Settings Designer, the feature you get in the C# or VB.NET that makes it easy to use settings. It is not like you can't reproduce it, carefully look at the changes made to the app.config file and the content of the Settings.Designer.cs file. Note how the [UserScopedSetting] attribute marks a setting that can be changed and saved at runtime and ends up in the `` section of the app.config file. It is just that you get no help whatsoever getting this right and small mistakes produce undiagnosable runtime errors. It is C++, it is supposed to be hard. – Hans Passant Dec 07 '14 at 13:28

1 Answers1

7

Follow this steps and it will work like desired:

1 - Add a reference to System.Configuration

2 - Add New Item > Visual C++ > Utility > Configuration file

3 - Open app.config and add your settings for example:

<configuration>
  <appSettings>
    <add key="greeting" value="Hallo world!" />
  </appSettings>
</configuration>

4 - Copy the app.config to the output folder in the post build event: goto Project Properties > Configuration Properties > Build Events > Post-Build Events > Command Line and add this:

copy app.config "$(TargetPath).config"

5 - Read your settings:

String^ greeting = ConfigurationManager::AppSettings["greeting"];
Console::WriteLine(greeting);

Here's an AppConfigDemo project in C++/CLI.

t3chb0t
  • 16,340
  • 13
  • 78
  • 118
  • I guess I didn't explain myself correctly. I want to use user.config, not app.config. I already know how to use app.config. The reason is because app is installed in Program Files\AppName, so app.config goes to that place. Eventually, I need to change some setting of the ones in app.config, so I need admin rights for doing that (because Program Files\* is read only for no admin users) Also, it is possible to have more than only one user, so I want that everyone could save its own configuration (but app.config is common to all users) – Juanma Lozano Dec 06 '14 at 17:47
  • I wouldn't do too much with C++/CLI but what is really necessary. Did you see this question about [How to save user.config to AppData\Roaming folder](http://stackoverflow.com/a/4298215/235671)? I'd go with a C# dll for the settings. – t3chb0t Dec 06 '14 at 18:21
  • thanks for your help but this question is already about location of user.config (in C#) and my problem is I don't know how to create it (and read/write, obviously) in first place. In C# (as I have read) VisualStudio makes all the work for you creating and managing the settings in user.config, but with C++ you are completely alone... – Juanma Lozano Dec 07 '14 at 11:42