English 中文(简体)
如何忽视“拒绝走这条道路”/C#中未经许可的Access例外?
原标题:How to ignore "Access to the path is denied" / UnauthorizedAccess Exception in C#?

How to bypass/ignore "Access to the path is denied"/UnauthorizedAccess exception

and continue to collecting filenames in this method;

public static string[] GetFilesAndFoldersCMethod(string path)
{
   string[] filenames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Select(Path.GetFullPath).ToArray();
   return filenames;
}

页: 1 ......

foreach (var s in GetFilesAndFoldersCMethod(@"C:/"))
{
    Console.WriteLine(s);
}

My application stops on the firstline of GetFilesAndFoldersCMethod and an exception says; "Access to the path C:@Logs is denied.". Please help me...

Thanks,

最佳回答

这样做的最佳途径是增加Try/Catch/2005/4,以处理例外情况。

try
{
   string[] filenames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Select(Path.GetFullPath).ToArray();
   return filenames;
}
catch (Exception ex)
{
   //Do something when you dont have access
   return null;//if you return null remember to handle it in calling code
}

也可以具体处理 如果你在这项职能中采用其他守则,你希望确保这一例外是导致其失败的准入例外(Directory.GetFiles功能就推翻了这一例外。

try
{
   //...
}
catch(UnauthorizedAccessException ex)
{
    //User cannot access directory
}
catch(Exception ex)
{
    //a different exception
}

EDIT: As pointed out in the comments below it appears you are doing a recursive search with the GetFiles function call. If you want this to bypass any errors and carry on then you will need to write your own recursive function. There is a great example here that will do what you need. Here is a modification which should be exactly what you need...

List<string> DirSearch(string sDir) 
{
   List<string> files = new List<string>();

   try  
   {
      foreach (string f in Directory.GetFiles(sDir)) 
      {
         files.Add(f);
      }

      foreach (string d in Directory.GetDirectories(sDir)) 
      {
         files.AddRange(DirSearch(d));
      }
   }
   catch (System.Exception excpt) 
   {
      Console.WriteLine(excpt.Message);
   }

   return files;
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签