8

我正在 Windows 8.1 上使用 C# .NET4.0 VS2010 开发 Windows 桌面应用程序。我使用 .NET 设置机制存储了一系列设置。这些具有用户范围,因此,当在应用程序中设置它们时,它们将写入 Users\username\AppData\Local\companyname\App.exe_URL_randomstuff\versionno\user.config。

这些设置包括一些我需要隐藏的用户注册信息。我的研究表明我应该能够使用 RsaProtectedConfigurationProvider 加密设置,但我发现的所有示例都与加密 app.config 而不是 user.config 相关(例如http://msdn.microsoft.com/en-us /library/system.configuration.rsaprotectedconfigurationprovider.aspx)。

因此,我的问题是 user.config 可以加密吗?如果可以,如何加密?我注意到,当我实例化一个 System.Configuration.Configuration 对象时,我可以将 ConfigurationUserLevel 设置为 PerUserRoamingAndLocal。当我通过调试器检查对象时,它似乎引用了正确的 user.config 文件,但是当我继续实例化 ConfigurationSection 以保护它时,它返回 null。代码如下所示:

System.Configuration.Configuration config =
                    ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.PerUserRoamingAndLocal);

ConfigurationSection connStrings = config.AppSettings;

connStrings.SectionInformation.ProtectSection(provider);

我在想 config.AppSettings 可能不正确,但我不确定用什么替换它。

非常感谢任何建议。

4

1 回答 1

8

现在可以工作了。我使用 ConfigurationUserLevel.PerUserRoamingAndLocal 访问我的 user.config 文件是正确的。问题出在 config.AppSettings 上。我在正确的轨道上用 config.GetSection("Progname.Properties.Settings") 替换它,但我命名错误。现在的工作代码如下:

System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.PerUserRoamingAndLocal);

ConfigurationSection connStrings = config.GetSection("userSettings/Progname.Properties.Settings");

connStrings.SectionInformation.ProtectSection(provider);

“Progname”是您的程序集的名称。感谢@neoistheone 和@hatchet 的输入。

于 2013-11-09T10:57:52.913 回答