我有一个使用Visual Studio 2005创建的默认应用程序设置。安装我的应用程序后,它只能以管理员身份运行,因为某些文件是在应用程序文件夹中编写的。
我发现在Visual Studio 2010上有一个属性可以在应用程序文件夹中的某些文件夹上更改此权限。
如何允许我的应用程序在应用程序文件夹中创建和编辑特定文件而不以管理员身份运行?
答案 0 :(得分:4)
以下是您的选择,假设您无法更改应用程序本身尝试读取/写入文件的位置:
编辑:这是一个直接来自我编写的应用程序的自定义操作的方法,该应用程序具有类似的“遗留”应用程序,该应用程序必须从配置文件读取/写入数据应用程序“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);
}
答案 1 :(得分:0)
不要向应用程序文件夹(程序文件下)写任何内容。而是使用用户的主文件夹,或者,如果你想写一些与机器上所有用户相关的东西 - ProgramData。
编辑:
如果您无法更改应用程序代码,则应尽可能避免将程序数据文件放在ProgramFiles下。还有一件事你可以做。您可以在c:\ ProgramData下创建一个文件夹,只需从C:\ Program Files ...创建一个符号链接即可。这样,您的遗留应用程序仍然可以找到它希望找到它的数据。
答案 2 :(得分:0)
您可以使用Windows资源管理器手动将NTFS文件夹权限更改为应用程序文件夹。但是,最佳做法是读/写不同的文件夹。