无法检查目录上的权限

时间:2014-01-22 13:11:25

标签: c# directory asp.net-3.5 unauthorizedaccessexcepti

我有一些代码,我希望能够捕获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

希望这会更有帮助回答。

0 个答案:

没有答案