简体   繁体   English

在Windows 8上无法访问App.Config的路径。如何更新App.Config?

[英]Access to path denied for App.Config on Windows 8. How can I update App.Config?

The following code works on Windows XP and Windows 7: 以下代码适用于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

But it's causing an Access to Path denied error on Windows 8. I've tried changing the ConfigurationUserLevel to PerUserRoamingAndLocal but it doesn't do anything. 但是,这导致Windows 8上出现“拒绝访问路径”错误。我尝试将ConfigurationUserLevel更改为PerUserRoamingAndLocal但是它什么也没做。

What do I need to do in order to be able to update the App.Config (or specifically, [application_name].exe.config in Windows 8? 为了能够更新App.Config(或具体地说,在Windows 8中为[application_name] .exe.config),我需要做什么?

I should add that the user trying running the app has the default Windows 8 privileges. 我应该补充一点,尝试运行该应用程序的用户具有默认的Windows 8特权。 I don't know exactly what that is, but it's pretty low. 我不知道那到底是什么,但是还很低。 Does this need to be elevated in order for this to work? 为了使它起作用,是否需要提高它?

Edit: The logged error: 编辑:记录的错误:

<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>

Can you post the exception message? 您可以发布异常消息吗?

I just tested your code on Win 8 Pro, VS 2012 and it works fine. 我刚刚在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); 

yields a config file of 产生一个配置文件

<?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>

As far as requiring elevated privileges, it depends on which user the application is running under and what the permissions are set to for the file/directory where your config file lives. 就要求提升的特权而言,它取决于应用程序在哪个用户下运行以及配置文件所在的文件/目录的权限设置为什么。

If you use the default project location %homepath%\\documents\\visual studio 2012\\projects\\, then you would not need to run the application with elevated privileges in order to edit the exe.config file. 如果您使用默认项目位置%homepath%\\ documents \\ visual studio 2012 \\ projects \\,则无需以提升的权限运行应用程序即可编辑exe.config文件。

If your file lives anywhere else, then you'll have to take a look at the privileges of that file (right click -> properties -> security tab) .. and see if the user running the application (likely your account) has write privileges. 如果您的文件位于其他任何地方,则必须查看该文件的特权(右键单击->属性->安全选项卡)..并查看运行该应用程序的用户(可能是您的帐户)是否已写入特权。

This is a great explanation on NTFS file permissions. 这是对NTFS文件权限的很好的解释。 http://www.techrepublic.com/article/windows-101-know-the-basics-about-ntfs-permissions/6084446 http://www.techrepublic.com/article/windows-101-know-the-basics-about-ntfs-permissions/6084446

Try this to remove the key: 尝试删除密钥:

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);

or to change the key value: 或更改键值:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM