1

我正在编写一个将在 Okuma 控件上运行并具有应用程序设置的应用程序。由于条件之一是必须轻松备份应用程序的设置,因此我将它们保存在应用程序目录中。它可以在控件上运行,因为应用程序转到 D: 但如果有人将应用程序安装在 C 驱动器上的 PC 上,则应用程序无法访问它自己的应用程序目录并出现错误。

条件:

  • Windows 7的
  • P300控制
  • 正在安装到 D 盘的应用程序
  • 如果有人安装到 PC 上的 C 盘,则必须工作

是否有放置所有应用程序设置的标准位置?

4

1 回答 1

2

继续将您的应用程序设置和其他数据保存在应用程序的安装目录中。无需为“仅限 PC”安装更改目录位置。

文件访问问题的解决方案是在安装期间更改文件权限。

例如,有人使用 WIX 安装程序发布的这个答案

这里回答了一个类似的问题。

您可以使用与此类似的代码在安装期间更改权限(当用户具有管理员权限时)

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);
    }
}
于 2014-05-08T14:35:03.243 回答