CommonApplicationData给出了不正确的权限

本文关键字:权限 不正确 CommonApplicationData | 更新日期: 2023-09-27 17:49:30

我一直在谷歌和回来试图弄清楚这个问题。让我告诉你问题所在,然后告诉你我正在做什么来解决这个问题。所以问题是,我安装我写的程序到另一台计算机只有一个帐户(内置的Admin帐户)。然后创建一个新的标准用户,并使用这个新用户运行程序。只要我不通过我的程序对配置文件(位于CommonApplicationData中的xml文件)进行任何更改,就没有问题。然而,如果我做了一个改变,那么我的程序崩溃与AccessDenied异常。我第一次遇到它,我只是去文件夹,并试图创建一个新的文本文件,所以我开始搜索高和低为什么我得到这个错误。现在,如果我以管理员身份登录删除配置文件,然后以标准用户身份登录并运行程序,配置文件将被重新创建,现在我的用户对该文件具有读/写访问权限。所以我的问题是,我如何从一开始就做到这一点?

我所尝试的是通过使用NUnit我写了一个测试,创建配置文件在完全相同的位置,如果我的程序要运行它。并断言我可以从它读和写,通过。奇怪的是,如果我以标准用户的身份创建具有特定安全选项集(如下代码)的文件夹,那么我的管理帐户将不再具有读取或写入文件的能力。

    [TestFixtureSetUp]
    public void Setup()
    {
        xmlPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MyCompany''MyProgram");
        xmlFileLocation = System.IO.Path.Combine(xmlPath, "MyConfig.xml");
        fileInfo = new FileInfo(xmlFileLocation);
    }
    [Test]
    public void TestCreateNewFolder()
    {
        Assert.IsFalse(fileInfo.Directory.Exists);
        Console.WriteLine("Creating new directory:{0}", fileInfo.DirectoryName);
        Console.WriteLine("Directory.FullName:{0}", fileInfo.Directory.FullName);
        SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
        NTAccount acct = sid.Translate(typeof(NTAccount)) as NTAccount;
        string strEveryoneAccount = acct.ToString();
        FileSystemAccessRule everyOne = new FileSystemAccessRule(strEveryoneAccount, FileSystemRights.FullControl, AccessControlType.Allow);
        DirectorySecurity dirSecurity = new DirectorySecurity(fileInfo.DirectoryName, AccessControlSections.Group);
        dirSecurity.AddAccessRule(everyOne);
        fileInfo.Directory.Create(dirSecurity);
    }

我变得超级沮丧,因为这似乎是一件微不足道的事情。"作为管理员将此新文件夹/子文件夹/文件标记为每个人的FullControl",以便在此之前或之后创建的任何新用户都可以读写此文件。我哪里做错了?

CommonApplicationData给出了不正确的权限

当我们想要授予文件夹完全控制权时,我们这样做是为了解决这个问题,我们使用以下命令。您应该能够从Wix中运行它:

icacls.exe "C:'ProgramData'MyCompany'MyApplication" /grant Users:(OI) (CI)F

我接受了Daniel Persson建议的想法,并在WIX中查找了等效的调用,最终是Permission EX. in Directory。wxs(同样的文件,我把片段为我的INSTALLFOLDER,我添加了另一个目录,如这样

        <!--Directory for Config file-->
        <Directory Id="CommonAppDataFolder">
            <Directory Id="commonAppDataMyCompany" Name="MyCompany"/>
        </Directory>

接下来,我创建了一个名为Permissions.wxs的新文件

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
    <Fragment>
        <ComponentGroup Id="ProductPermissions">
            <Component Id="INS_COMP" DiskId="1" Guid="{YOUR-GUID}"  Directory="INSTALLFOLDER">
                <CreateFolder Directory="INSTALLFOLDER">
                    <util:PermissionEx User="Users" GenericAll="yes"/>
                </CreateFolder>
            </Component>
            <Component Id="CMN_APP" DiskId="1" Guid="{YOUR-GUID}"  Directory="commonAppDataMyCompany">
                <CreateFolder Directory="commonAppDataMyCompany">
                    <util:PermissionEx User="Users" GenericAll="yes"/>
                </CreateFolder>
            </Component>
        </ComponentGroup>
    </Fragment>
</Wix>

最后要做的是引用新的组件组。

    <!--Features to add to cab file-->
    <Feature Id="ProductFeature" Title="Keymon Setup" Level="1">
        <ComponentGroupRef Id="ProductComponents" />
        <ComponentGroupRef Id="ProductContentComponents" />
        <ComponentGroupRef Id="RegistrySetings" />
        <ComponentGroupRef Id="ProductPermissions"/>
        <ComponentRef Id="ApplicationShortcut"/>
    </Feature>
好了,现在我的。我的常用app数据文件夹继承了权限,我的INSTALL文件夹也是,这是我们IT部门让人做的。现在他们不需要再这样做了:)