[英]Permissions on Application Folder
我有一個使用Visual Studio 2005創建的默認應用程序設置。安裝我的應用程序后,它只能以管理員身份運行,因為某些文件是在應用程序文件夾中編寫的。
我發現在Visual Studio 2010上有一個屬性可以在應用程序文件夾中的某些文件夾上更改此權限。
如何允許我的應用程序在應用程序文件夾中創建和編輯特定文件而不以管理員身份運行它?
以下是您的選擇,假設您無法更改應用程序本身嘗試讀取/寫入文件的位置:
編輯:這是一個直接出於安裝程序的自定義操作的方法,我編寫的應用程序有一個類似的“遺留”應用程序,它必須從應用程序“home”目錄的子文件夾中的配置文件讀取/寫入數據。 傳入的IDictionary是您從各種自定義操作方法(OnBeforeInstall,OnAfterInstall,OnCommit等)獲得的IDictionary,因此您只需將其放入Installer類,從處理程序中為您選擇的安裝事件調用它(必須在安裝程序完成文件系統更改后,並調用它:
private void SetEditablePermissionOnConfigFilesFolder(IDictionary savedState)
{
if (!Context.Parameters.ContainsKey("installpath")) return;
//Get the "home" directory of the application
var path = Path.GetFullPath(Context.Parameters["installpath"]);
//in my case the necessary files are under a ConfigFiles folder;
//you can do something similar with individual files
path = Path.Combine(path, "ConfigFiles");
var dirInfo = new DirectoryInfo(path);
var accessControl = dirInfo.GetAccessControl();
//Give every user of the local machine rights to modify all files
//and subfolders in the directory
var userGroup = new NTAccount("BUILTIN\\Users");
var userIdentityReference = userGroup.Translate(typeof(SecurityIdentifier));
accessControl.SetAccessRule(
new FileSystemAccessRule(userIdentityReference,
FileSystemRights.Modify,
InheritanceFlags.ObjectInherit
| InheritanceFlags.ContainerInherit,
PropagationFlags.None,
AccessControlType.Allow));
//Commit the changes.
dirInfo.SetAccessControl(accessControl);
}
不要在應用程序文件夾中寫入任何內容(在程序文件下)。 而是使用用戶的主文件夾,或者,如果你想寫一些與機器上所有用戶相關的東西 - ProgramData。
編輯:
如果您無法更改應用程序代碼,則應盡可能避免將程序數據文件放在ProgramFiles下。 還有一件事你可以做。 您可以在c:\\ ProgramData下創建一個文件夾,只需從C:\\ Program Files ...創建一個符號鏈接即可。 這樣,您的遺留應用程序仍然可以找到它希望找到它的數據。
您可以使用Windows資源管理器手動將NTFS文件夾權限更改為應用程序文件夾。 但是,最佳做法是讀/寫不同的文件夾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.