我正在编写一个在Okuma控件上运行并具有应用程序设置的应用程序。因为其中一个条件是必须很容易地备份应用程序的设置,所以我将它们保存在应用程序目录中。它在控件上工作,因为应用程序转到D:但是如果有人在C驱动器上的PC上安装应用程序,应用程序无法访问它自己的应用程序目录,并且会出现错误。
条件:
是否有一个标准点来放置所有应用程序设置?
发布于 2014-05-08 14:35:03
继续将应用程序设置和其他数据保存在应用程序的安装目录中。没有必要只为“只安装PC”而更改目录位置。
文件访问问题的解决方案是在安装期间更改文件权限。
例如,this answer someone posted using WIX installer。
类似的问题is answered here。
您可以使用类似于此的代码在安装期间更改权限(当用户拥有管理权限时)。
using System.Security.Principal;
public static void SetPermissions()
{
String path = GetPath();
try
{
// Create security idenifier for all users (WorldSid)
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
DirectoryInfo di = new DirectoryInfo(path);
DirectorySecurity ds = di.GetAccessControl();
// add a new file access rule w/ write/modify for all users to the directory security object
ds.AddAccessRule(new FileSystemAccessRule(sid,
FileSystemRights.Write | FileSystemRights.Modify,
InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, // all sub-dirs to inherit
PropagationFlags.None,
AccessControlType.Allow)); // Turn write and modify on
// Apply the directory security to the directory
di.SetAccessControl(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}https://stackoverflow.com/questions/23544161
复制相似问题