//====
//添加权限
private void SetAttributes(string folder)
{
if (folder == "" || !Directory.Exists(folder))
{
return;
}
DirectoryInfo folderInfo = new DirectoryInfo(folder);
//添加当前用户权限
string curUser = Environment.UserDomainName.ToString() + "\\" + Environment.UserName.ToString();
DirectorySecurity tmpSec = folderInfo.GetAccessControl();
tmpSec.SetAccessRule(new FileSystemAccessRule(curUser, FileSystemRights.Modify, AccessControlType.Allow));
//设置文件权限
FileInfo[] fileInfos = folderInfo.GetFiles();
if (fileInfos != null && fileInfos.Length > 0)
{
foreach (FileInfo fileInfo in fileInfos)
{
FileSecurity tmpFilesec = fileInfo.GetAccessControl();
tmpFilesec.SetAccessRule(new FileSystemAccessRule(curUser, FileSystemRights.Modify, AccessControlType.Allow));
fileInfo.Attributes = FileAttributes.Normal;
}
}
//递归
DirectoryInfo[] folderInfos = folderInfo.GetDirectories();
if (folderInfos != null && folderInfos.Length > 0)
{
foreach (DirectoryInfo childFolderInfo in folderInfos)
{
this.SetAttributes(childFolderInfo.FullName);
}
}
}