2012-12-04 26 views
2

下面的代码适用于Windows XP和Windows 7:访问Windows 8上App.Config的路径被拒绝。如何更新App.Config?

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到路径拒绝访问错误,我试图改变ConfigurationUserLevelPerUserRoamingAndLocal但它不会做任何事情。

需要什么条件才能做才能够更新App.Config中(或者具体来说,[应用程序名称]在Windows 8 .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

你能后的异常信息?

我只是测试在Win 8专业版,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

+0

我已经更新了异常消息和堆栈跟踪的问题。 – DaveDev

+0

刚刚从看路,C:/ ProgramData是只读的,默认情况下所有,但内置的系统帐户和Administrators组。在提升的特权下运行会解决您的问题,或者您可以明确授予您的用户帐户编辑文件的权限。我可能会走明确给予你自己特权的路线。 –

+0

应用程序是否可以自动给用户帐户明确的权限,或者是否必须手动设置?当这个应用程序部署时,它会转到非技术型的用户,所以我需要尽可能简化。如果没有办法自动执行此操作,则必须将该配置数据放在其他位置。 – DaveDev

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