我爲一些簡單任務開發了Winforms應用程序。此應用程序具有保存一些設置的App.config文件。我在應用程序中創建了一個標籤頁,我可以使用Configuration類來調整配置文件的設置。我使用應用程序本身中的「保存」按鈕保存到App.config中,該應用程序使用Configuration.Save。Configuration.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()
我用來做這樣的事情,但從來沒有見過這個問題。我在文件中有用戶設置和應用程序設置 - 但是當我保存它時總是到AppData文件夾。有沒有一個堆棧跟蹤? – Andez
你爲什麼要保存在AppData文件夾中?我使用的.config文件總是與.exe一起,所以在網絡驅動器本身中,而不是在AppData中。這是你的環境和我的區別我認爲 – Ozkan
我面臨同樣的問題,請讓我知道你是如何解決它? –