我一直在谷歌,并试图解决这个问题。让我告诉你问题然后告诉/告诉你我正在努力解决这个问题。所以问题是我将我写的程序安装到只有一个帐户的另一台计算机(内置管理员帐户)。然后我创建一个新的标准用户,并使用这个新用户运行该程序。只要我不通过我的程序对配置文件(位于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);
}
我变得非常沮丧,因为这看起来像是一件微不足道的事情。 “由于管理员将此新文件夹/子文件夹/文件标记为Everyone的FullControl”,以便在之前或之后创建的任何新用户都可以读取和写入此文件。我哪里错了?
答案 0 :(得分:1)
当我们想要对文件夹授予完全控制权时,我们会使用以下命令来解决此问题。你应该可以从Wix运行它:
icacls.exe "C:\ProgramData\MyCompany\MyApplication" /grant Users:(OI) (CI)F
答案 1 :(得分:0)
我认为Daniel Persson建议并查找WIX中的等效调用,最终成为Permission EX。在Directory.wxs中(我为我的INSTALLFOLDER添加了Fragment的同一个文件,我添加了另一个目录,如此
<!--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>
我们去了,现在我的公共应用程序数据文件夹都继承了权限,我的INSTALL文件夹也是如此,这是我们的IT部门让人们做的事情。现在他们不应该再这样做了:)