我有一些代码,我希望能够捕获 System.UnauthorizedAccessException 和任何其他异常,但它似乎没有按预期工作。
代码是:
public IEnumerable<string> GetFilesToShred(string xmlDirectory)
{
try
{
return Directory.GetFiles(xmlDirectory, "*.xml");
}
catch (Exception ex)
{
Log.Error("Could not retrieve list of files to shred: " + ex.Message, ex);
return null;
}
}
当我将此代码提供给用户无权访问的 xml 目录时,catch 语句会被删除,并且异常会被进一步抛出堆栈。
知道我可能做错了什么吗?
更新:根据要求,请在下面找到完整的堆栈跟踪:
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)
at System.IO.StreamReader..ctor(String path, Boolean detectEncodingFromByteOrderMarks)
at XmlShredder.ShredderGateway.ShredAndWrite(String XmlDirectory, IEnumerable`1 XmlInputFiles, String outputDirectory, Boolean isDebug) in c:\Dev\XmlShredder\XmlShredder\ShredderGateway.cs:line 102
希望这将有助于回答。