[英]C# File Permissions
我目前正在用C#编写一个程序,该程序会将所有用户配置文件复制到外部设备(在这种情况下,是我的家庭服务器)。
当我的代码遍历文件和文件夹时,它将引发UnauthorizedAccessException
。
我已经对此进行了Google搜索,并搜索了StackOverflow,但是我找不到一个不涉及终止过程的明确解决方案。 这个想法是它应该复制具有读取权限的文件和文件夹。
刚开始时就具有此功能,但我通过限制要备份的目录轻松地解决了该问题(尽管我更喜欢完全备份)。
这是我的代码:
FileInfo f = new FileInfo(_configuration.Destination);
if (!f.Directory.Exists)
{
f.Directory.Create();
}
string[] backupDirectories = new string[]
{
"Desktop",
"Documents",
"Downloads",
"Favorites",
"Links",
"Pictures",
"Saved Games",
"Searches",
"Videos",
".git",
".android",
".IdealC15",
".nuget",
".oracle_jre_usage",
".vs",
"Contacts"
};
foreach (string dirPath in backupDirectories)
{
DirectoryInfo dirInfo = new DirectoryInfo(_path + "\\" + dirPath);
if (dirInfo.Exists)
{
foreach (string dirP in Directory.GetDirectories(dirInfo.FullName, "*", SearchOption.AllDirectories))
{
DirectoryInfo dirI = new DirectoryInfo(dirP);
if (dirI.Exists)
{
string dir = dirP.Replace(_path, _configuration.Destination);
try
{
Directory.CreateDirectory(dir);
textBox2.Invoke((MethodInvoker) delegate
{
textBox2.AppendText("Create Directory: " + dir + Environment.NewLine);
});
} catch (Exception e)
{
textBox2.Invoke((MethodInvoker) delegate
{
textBox2.AppendText("Could NOT Create Directory: " + dir + Environment.NewLine);
});
continue;
}
foreach (FileInfo theFile in dirInfo.GetFiles("*", SearchOption.AllDirectories))
{
string newPath = theFile.FullName;
string file = newPath.Replace(_path, _configuration.Destination);
try
{
File.Copy(newPath, file, true);
textBox2.Invoke((MethodInvoker) delegate
{
textBox2.AppendText("Create File: " + file + Environment.NewLine);
});
} catch (Exception ex)
{
textBox2.Invoke((MethodInvoker) delegate
{
textBox2.AppendText("Could NOT Create File: " + file + Environment.NewLine);
});
}
}
}
}
}
}
如果代码凌乱,我深表歉意,但我将描述其工作方式。 第一位检查备份文件夹在外部驱动器上是否存在。
第二部分说明了我需要备份哪些文件夹(如果您能够解决此问题并使它备份所有具有权限的目录,请帮助我这样做)。
第一个循环为每个backupDirectories
开始迭代。 第二个循环开始备份目录中每个目录的迭代。 第三个循环为备份目录中的每个文件夹启动迭代。
在Directory.GetDirectories(dirInfo.FullName, "*", SearchOption.AllDirectories)
处引发异常,并且它试图访问C:\\Users\\MyName\\Documents\\My Music
。 尝试在资源管理器中访问它确实给了我一个权限错误,尽管当我尝试转到“文档”时(我在Windows 10 Pro中)它没有在资源管理器中列出。
正如我建议的那样,由于Operating System
权限高于应用程序,因此您可能无法做的事情超出Operating System
允许您执行的操作(即访问或不访问某些folder
)。
因此,在Operating System
级别最好解决文件夹的可访问性。
但是您仍然可以在程序级别做两件事,以最大程度地减少搜索项目时的损坏。
在对目录进行任何查询之前,使用Directory.AccessControl
知道Directory.AccessControl
的访问级别。 这不是那么容易,并且这里和这里都有详尽的答案。
为了尽量减少使用未经授权的访问问题上取得的损害SearchOption.TopDirectoryOnly
代替SearchOption.AllDirectories
,与所有访问的目录递归搜索相结合。 这就是你如何编码
public static List<string> GetAllAccessibleDirectories(string path, string searchPattern) { List<string> dirPathList = new List<string>(); try { List<string> childDirPathList = Directory.GetDirectories(path, searchPattern, SearchOption.TopDirectoryOnly).ToList(); //use TopDirectoryOnly if (childDirPathList == null || childDirPathList.Count <= 0) //this directory has no child return null; foreach (string childDirPath in childDirPathList) { //foreach child directory, do recursive search dirPathList.Add(childDirPath); //add the path List<string> grandChildDirPath = GetAllAccessibleDirectories(childDirPath, searchPattern); if (grandChildDirPath != null && grandChildDirPath.Count > 0) //this child directory has children and nothing has gone wrong dirPathList.AddRange(grandChildDirPath.ToArray()); //add the grandchildren to the list } return dirPathList; //return the whole list found at this level } catch { return null; //something has gone wrong, return null } }
上面的功能最大程度地减少了未经授权仅访问有问题的子目录所造成的损害。 可以返回所有其他可访问目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.