我有一段设置文件夹权限的代码,是放在Windows Service中执行的,代码如下:try
{
System.Security.AccessControl.DirectorySecurity fSec;
string user = "users";
foreach (string dir in writableDir)
{
if (string.IsNullOrEmpty(dir)) continue;
path = dir.StartsWith("\\") ? mDir + dir : mDir + "\\" + dir;
fSec = new System.Security.AccessControl.DirectorySecurity();
//对文件夹本身、子文件夹及文件都要加上此权限,所以需要传不同的InheritanceFlags参数设置3次权限
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Read,InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly,AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Read, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Read, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)); fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly,AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
System.IO.Directory.SetAccessControl(path, fSec);
}
}
catch (Exception ex)
{
EventLog.WriteEntry("IISMSLog", ex.Message + ". Path:" + path);
return ex.Message;
}其中mDir为一父目录,如:E:\test\
writableDir为一个List<string>,其中的内容为要设置权限的文件夹相对路径,如:
uploadfiles\
customfaces\现在的问题是,Windows服务中执行这段代码,为什么连mDir也加上了写权限,默认的mDir权限是只读的
我在控制台程序中复制这段程序执行却不会有错?
{
System.Security.AccessControl.DirectorySecurity fSec;
string user = "users";
foreach (string dir in writableDir)
{
if (string.IsNullOrEmpty(dir)) continue;
path = dir.StartsWith("\\") ? mDir + dir : mDir + "\\" + dir;
fSec = new System.Security.AccessControl.DirectorySecurity();
//对文件夹本身、子文件夹及文件都要加上此权限,所以需要传不同的InheritanceFlags参数设置3次权限
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Read,InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly,AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Read, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Read, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow)); fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly,AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
System.IO.Directory.SetAccessControl(path, fSec);
}
}
catch (Exception ex)
{
EventLog.WriteEntry("IISMSLog", ex.Message + ". Path:" + path);
return ex.Message;
}其中mDir为一父目录,如:E:\test\
writableDir为一个List<string>,其中的内容为要设置权限的文件夹相对路径,如:
uploadfiles\
customfaces\现在的问题是,Windows服务中执行这段代码,为什么连mDir也加上了写权限,默认的mDir权限是只读的
我在控制台程序中复制这段程序执行却不会有错?
如果dir是空时,是不是 path = mDir,就改了权限
if (string.IsNullOrEmpty(dir)) continue;
path = dir.StartsWith("\\") ? mDir + dir : mDir + "\\" + dir;
--------------------------------------
怎么看还是觉得这两句有问题,改一下试
if (!string.IsNullOrEmpty(dir))
{
path = dir.StartsWith("\\") ? mDir + dir : mDir + "\\" + dir;
}
我怀疑是这个地方的问题:
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly,AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, InheritanceFlags.None, PropagationFlags.InheritOnly, AccessControlType.Allow));
fSec.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
设置继承关系的时候错了,我查了MSDN对第3、4个参数的解释,看得不是很懂
谁对这个地方比较熟悉解释一下