Vic*_*tor 1 c# permissions windows-installer visual-studio-2005
我有使用Visual Studio 2005创建的默认应用程序设置。安装我的应用程序后,它只能以管理员身份运行,因为某些文件写入了“应用程序文件夹”中。
我发现在Visual Studio 2010上,有一个属性可以更改应用程序文件夹内某些文件夹的此权限。
如何允许我的应用程序在不以admin身份运行的情况下在应用程序文件夹中创建和编辑特定文件?
如果您不能更改应用程序本身尝试读取/写入文件的位置,则可以使用以下选项:
编辑:这是一种针对我编写的应用程序的安装程序的自定义操作的方法,该应用程序具有类似的“旧版”应用程序,该应用程序必须从该应用程序“主”目录的子文件夹中的配置文件读取/写入数据。传入的IDictionary是从各种自定义操作方法(OnBeforeInstall,OnAfterInstall,OnCommit等)中获得的,因此您只需将其放入Installer类,并从处理程序中为您选择的install事件调用它(必须在安装程序完成文件系统更改之后),然后调用它:
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);
}
Run Code Online (Sandbox Code Playgroud)