2

以下代码适用于 Windows XP 和 Windows 7:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);
config.Save(ConfigurationSaveMode.Modified); // fails here

但这会导致 Windows 8 上的 Access to Path denied 错误。我尝试将其更改为ConfigurationUserLevelPerUserRoamingAndLocal但它没有做任何事情。

我需要做什么才能更新 App.Config(或者具体来说,Windows 8 中的 [application_name].exe.config?

我应该补充一点,尝试运行该应用程序的用户具有默认的 Windows 8 权限。我不知道那是什么,但它非常低。是否需要提升才能使其正常工作?

编辑: 记录的错误:

<Message>Access to the path 'C:\ProgramData\Path\AppName.exe.config' is denied.</Message>
<StackTrace>   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.Configuration.Internal.WriteFileContext.ValidateWriteAccess(String filename)
   at System.Configuration.Internal.WriteFileContext.Complete(String filename, Boolean success)
   at System.Configuration.Internal.InternalConfigHost.StaticWriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
   at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext, Boolean assertPermissions)
   at System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
   at System.Configuration.Internal.DelegatingConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
   at System.Configuration.UpdateConfigHost.WriteCompleted(String streamName, Boolean success, Object writeContext)
   at System.Configuration.MgmtConfigurationRecord.SaveAs(String filename, ConfigurationSaveMode saveMode, Boolean forceUpdateAll)
   at System.Configuration.Configuration.SaveAsImpl(String filename, ConfigurationSaveMode saveMode, Boolean forceSaveAll)
   at System.Configuration.Configuration.Save(ConfigurationSaveMode saveMode)
   at MyNamespace.Infrastructure.ConfigurationManager.WriteToAppSettings(String key, String value) in C:\Path\To\App\AppName\Infrastructure\ConfigurationManager.cs:line 76
</StackTrace>
4

2 回答 2

4

您可以发布异常消息吗?

我刚刚在 Win 8 Pro、VS 2012 上测试了你的代码,它运行良好。

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove("my_new_key");
config.AppSettings.Settings.Add("my_new_key", "my new value");
config.Save(ConfigurationSaveMode.Modified); 

产生一个配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="my_new_key" value="my new value" />
    </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

至于需要提升的权限,这取决于应用程序在哪个用户下运行以及为您的配置文件所在的文件/目录设置的权限。

如果您使用默认项目位置 %homepath%\documents\visual studio 2012\projects\,则无需以提升的权限运行应用程序即可编辑 exe.config 文件。

如果您的文件位于其他任何地方,那么您必须查看该文件的权限(右键单击 -> 属性 -> 安全选项卡).. 并查看运行应用程序的用户(可能是您的帐户)是否已写入特权。

这是对 NTFS 文件权限的一个很好的解释。 http://www.techrepublic.com/article/windows-101-know-the-basics-about-ntfs-permissions/6084446

于 2012-12-04T11:07:37.207 回答
1

试试这个来删除密钥:

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

AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
appSettings.Settings.Remove("ConnectionString");
appSettings.Settings.Add("ConnectionString", "");

config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

或更改键值:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection settings = config.AppSettings.Settings;

settings["KeyName"].Value = "newkeyvalue";

config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
于 2012-12-04T11:09:26.717 回答