在Windows 8上无法访问App.Config的路径.如何更新App.Config? [英] Access to path denied for App.Config on Windows 8. How can I update App.Config?

查看:87
本文介绍了在Windows 8上无法访问App.Config的路径.如何更新App.Config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可在Windows XP和Windows 7上运行:

  var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);config.AppSettings.Settings.Remove(key);config.AppSettings.Settings.Add(键,值);config.Save(ConfigurationSaveMode.Modified);//这里失败 

但是这会导致Windows 8上的拒绝访问路径"错误.我尝试将 ConfigurationUserLevel 更改为 PerUserRoamingAndLocal ,但是它无能为力.

要更新App.Config(或Windows 8中的[application_name] .exe.config),我需要做什么?

我应该补充一点,尝试运行该应用程序的用户具有默认的Windows 8特权.我不确切知道那是什么,但是还很低.为了使此功能有效,是否需要提高它们?

修改:记录的错误:

</Message>

 < Message>拒绝访问路径'C:\ ProgramData \ Path \ AppName.exe.config'.< StackTrace>在System.IO .__ Error.WinIOError(Int32 errorCode,可能是StringFullPath)在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy,布尔useLongPath,布尔checkHost)在System.IO.FileStream..ctor处(字符串路径,FileMode模式,FileAccess访问,FileShare共享)在System.Configuration.Internal.WriteFileContext.ValidateWriteAccess(字符串文件名)在System.Configuration.Internal.WriteFileContext.Complete处(字符串文件名,布尔值成功)在System.Configuration.Internal.InternalConfigHost.StaticWriteCompleted(String streamName,布尔成功,对象writeContext,布尔assertPermissions)在System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName,布尔成功,对象writeContext,布尔assertPermissions)在System.Configuration.Internal.InternalConfigHost.System.Configuration.Internal.IInternalConfigHost.WriteCompleted(String streamName,布尔值成功,对象writeContext)在System.Configuration.Internal.DelegatingConfigHost.WriteCompleted(String streamName,布尔值成功,对象writeContext)在System.Configuration.UpdateConfigHost.WriteCompleted(String streamName,布尔值成功,对象writeContext)在System.Configuration.MgmtConfigurationRecord.SaveAs处(字符串文件名,ConfigurationSaveMode saveMode,布尔值forceUpdateAll)在System.Configuration.Configuration.SaveAsImpl(String filename,ConfigurationSaveMode saveMode,Boolean forceSaveAll)在System.Configuration.Configuration.Save(ConfigurationSaveMode saveMode)在MyNamespace.Infrastructure.ConfigurationManager.WriteToAppSettings(字符串键,字符串值)在C:\ Path \ To \ App \ AppName \ Infrastructure \ ConfigurationManager.cs:第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",我的新值");config.Save(ConfigurationSaveMode.Modified); 

产生一个配置文件

 <?xml version ="1.0" encoding ="utf-8"吗?><配置>< appSettings><添加key ="my_new_key" value =我的新值"/></appSettings><启动>< 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

The following code works on Windows XP and 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.

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?

I should add that the user trying running the app has the default Windows 8 privileges. 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.

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.

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. http://www.techrepublic.com/article/windows-101-know-the-basics-about-ntfs-permissions/6084446

这篇关于在Windows 8上无法访问App.Config的路径.如何更新App.Config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆