我想知道如何测试我是否可以访问字符串路径.这是我使用的代码:
using System;
using System.IO;
namescpace prog1
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Users\Admin";
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo fil in dir.GetFiles())
{
//At dir.GetFiles, I get an error saying
//access to the string path is denied.
Console.WriteLine(fil.Name);
}
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想测试访问是否被拒绝(到字符串路径)然后执行GetFiles和所有这些.
我已经发现了这个问题:如何轻松检查.NET中的文件是否被拒绝访问?
有帮助吗?
最简单(通常最安全)的选项是执行您现在正在执行的操作,但将代码包装在适当的异常处理中.
然后,您可以显式地从(并且可能来自构造函数,具体取决于路径)捕获UnauthorizedAccessException,并将处理逻辑放在那里.GetFilesSecurityExceptionDirectoryInfo
| 归档时间: |
|
| 查看次数: |
3694 次 |
| 最近记录: |