以下代码适用于 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 上导致访问路径被拒绝错误。我尝试将 ConfigurationUserLevel
更改为 PerUserRoamingAndLocal
但它没有执行任何操作。
我需要做什么才能更新 App.Config(或者具体来说,Windows 8 中的 [application_name].exe.config?
我应该补充一点,尝试运行该应用程序的用户拥有默认的 Windows 8 权限。我不知 Prop 体是什么,但它相当低。是否需要提升才能使其发挥作用?
编辑: 记录的错误:
<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>
最佳答案
您可以发布异常消息吗?
我刚刚在 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
关于c# - Windows 8 上 App.Config 的路径访问被拒绝。如何更新 App.Config?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13701306/