2015-08-28 115 views
2

我爲一些簡單任務開發了Winforms應用程序。此應用程序具有保存一些設置的App.config文件。我在應用程序中創建了一個標籤頁,我可以使用Configuration類來調整配置文件的設置。我使用應用程序本身中的「保存」按鈕保存到App.config中,該應用程序使用Configuration.SaveConfiguration.Save在網絡驅動器上

但我在保存到網絡文件夾的配置文件時遇到了麻煩。當我將設置保存到本地文件夾中的文件(如:C:\)時,一切正常。我的意思是我將.exe和.config複製到網絡文件夾(例如:\\ folder \ application.exe),當我嘗試使用Configuration.Save將設置保存到配置文件時,出現以下錯誤:

System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.

編輯:或者我可以只使用StreamWriter而不是配置?

編輯2:

這是堆棧跟蹤:

System.UnauthorizedAccessException: Attempted to perform an unauthorized operation. 
    at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType type, String name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl) 
    at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext) 
    at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections, Object exceptionContext) 
    at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath) 
    at System.Configuration.Internal.WriteFileContext.DuplicateTemplateAttributes(String source, String destination) 
    at System.Configuration.Internal.WriteFileContext.DuplicateFileAttributes(String source, String destination) 
    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) 
    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 ToolNameSpace.Configs.Save() 
+0

我用來做這樣的事情,但從來沒有見過這個問題。我在文件中有用戶設置和應用程序設置 - 但是當我保存它時總是到AppData文件夾。有沒有一個堆棧跟蹤? – Andez

+1

你爲什麼要保存在AppData文件夾中?我使用的.config文件總是與.exe一起,所以在網絡驅動器本身中,而不是在AppData中。這是你的環境和我的區別我認爲 – Ozkan

+0

我面臨同樣的問題,請讓我知道你是如何解決它? –

回答

0

該文件可能已在該位置作爲管理員這將使其無法使用任何其他非管理用戶創建。

你需要設置文件權限讓大家喜歡這個

private void CreateSettingsFile(string path) 
    { 
     XDocument document = new XDocument(); 
     XElement rootElement = new XElement("settings"); 
     document.Add(rootElement); 
     document.Save(path); 
     var accessControl = File.GetAccessControl(path); 
     var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); 
     accessControl.AddAccessRule(new FileSystemAccessRule(everyone, 
                  FileSystemRights.FullControl | 
                  FileSystemRights.Synchronize, 
                  AccessControlType.Allow)); 

     manager = new XmlSettingsManager(document,() => XDocument.Load("file://" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, this.settingsPath)), 
      x => x.Save(this.settingsPath)); 
     File.SetAccessControl(path, accessControl); 
    } 
+0

XmlSettingsManager?這是否包含在.NET Framework中?但是,我可以應用我認爲的訪問控制,但它不會改變任何行爲。我不斷收到錯誤 – Ozkan

+0

抱歉,忘了提及您需要刪除該文件,並讓應用程序在標準憑據下創建它,然後才能再次運行它 –

+0

對不起,那是從我的自定義類中,設置文件可以是使用XDocument.Load(...)讀取 –