Van*_*dze 3 c# directoryinfo special-folders
我的目标是检查 DirectoryInfo.FullName 是否是特殊文件夹之一。
这就是我正在做的事情(检查每个特殊文件夹的directoryInfo.FullName,如果它们相等):
DirectoryInfo directoryInfo = new DirectoryInfo("Directory path");
if (directoryInfo.FullName == Environment.GetFolderPath(Environment.SpecialFolder.Windows) ||
directoryInfo.FullName == Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles ||)
...
...
)
{
// directoryInfo is the special folder
}
Run Code Online (Sandbox Code Playgroud)
但还有很多特殊的文件夹(Cookies、ApplicationData、InternetCache 等)。有什么办法可以更有效地完成这项任务吗?
谢谢。
尝试以下代码:
bool result = false;
DirectoryInfo directoryInfo = new DirectoryInfo("Directory path");
foreach (Environment.SpecialFolder suit in Enum.GetValues(typeof(Environment.SpecialFolder)))
{
if (directoryInfo.FullName == Environment.GetFolderPath(suit))
{
result = true;
break;
}
}
if (result)
{
// Do what ever you want
}
Run Code Online (Sandbox Code Playgroud)
希望这有帮助。
| 归档时间: |
|
| 查看次数: |
2503 次 |
| 最近记录: |