2010-10-13 110 views
6

我在Visual Studio中構建了一個程序。程序創建一個日誌文件並在程序運行時寫入日誌文件。因此我構建了一個安裝程序(setup-project),它應該爲我的程序文件夾設置寫入權限,而不管哪個用戶使用該程序。 目前它看起來像這樣:爲我的程序文件夾的所有用戶設置寫入權限

// ... 
} 
    InitializeComponent(); 

    string folder = Directory.GetCurrentDirectory(); 

    DirectorySecurity ds = Directory.GetAccessControl(folder); 
    ds.AddAccessRule(new FileSystemAccessRule("Everyone", //Everyone is important 
                //because rights for all users! 
    FileSystemRights.Read | FileSystemRights.Write, AccessControlType.Allow)); 
} 
// ... 

在最後兩行,我得到一個System.SystemException:「死Vertrauensstellung zwischen德primärenDomäneund明鏡vertrauenswürdigenDomänekonnte nicht hergestellt werden。」

[翻譯道:「主域和信任域之間的信任關係不能建立「]

堆棧跟蹤讀取這樣的:

bei System.Security.Principal.NTAccount.TranslateToSids(IdentityReferenceCollection sourceAccounts, Boolean& someFailed) 
bei System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean& someFailed) 
bei System.Security.Principal.NTAccount.Translate(IdentityReferenceCollection sourceAccounts, Type targetType, Boolean forceSuccess) 
bei System.Security.Principal.NTAccount.Translate(Type targetType) 
bei System.Security.AccessControl.CommonObjectSecurity.ModifyAccess(AccessControlModification modification, AccessRule rule, Boolean& modified) 
bei System.Security.AccessControl.CommonObjectSecurity.AddAccessRule(AccessRule rule) 
bei System.Security.AccessControl.FileSystemSecurity.AddAccessRule(FileSystemAccessRule rule) 

你知道我能做什麼嗎? 謝謝

+4

您是否使用管理員權限執行安裝程序? – Dennis 2010-10-13 08:55:27

+3

請注意,給定系統上的各種公用文件夾存儲程序數據是有原因的...... – NotMe 2010-10-13 09:03:49

+0

哦,我認爲使用管理員權限執行安裝程序是默認設置。我在哪裏可以找到這個?在我的設置項目的屬性中,我找不到這個。 – Rotaney 2010-10-13 09:25:02

回答

3

也許最好的答案不是你要求的。不寫入程序文件目錄是有很好的理由的。特別是日誌數據是暫時的,不應該寫在這裏。

將日誌數據寫入TEMP環境變量指定的目錄是一個好主意。如果你這樣做,你會爲用戶節省一些麻煩,並防止他們將來詛咒你的軟件。請看看這個答案,涵蓋了同一主題:

Allow access permission to write in Program Files of Windows 7

0

previously asked question應該指向你在正確的方向。基本上,你做不是想要任何用戶寫入Program Files文件夾。 UAC,安全和其他措施儘可能地嘗試和防止這種情況。基本上,如果你想要一個文件將被所有用戶寫入,你會希望它在ProgramData文件夾中,可以通過%ALLUSERSPROFILE%訪問,而不是個人用戶的臨時文件夾,這絕對是你想要的處理日誌文件。請記住,臨時文件夾的內容應該被認爲是易失性的,並且可以隨時刪除,例如通過「磁盤清理嚮導」。

3

您是否錯過了您實際將訪問控制設置回目錄的語句?

Directory.SetAccessControl(Directory.GetCurrentDirectory(), ds); 
相關問題