Note that there are some explanatory texts on larger screens.

plurals
  1. POSystem.UnauthorizedAccessException when getting directories
    text
    copied!<p>I am really new to C#, so I've been working on a small pet project.</p> <p>I've created a small program that compares the size of a directory with a given size. And if the directory is equal or larger, then it logs the path to that directory.</p> <pre><code>long size = Convert.ToInt32(Size) * 1024 * 1024; string[] directories = Directory.GetDirectories(path, "*", SearchOption.AllDirectories); //the error occurs on this line Array.Sort(directories); foreach (string name in directories) try { DirectoryInfo directory = new DirectoryInfo(name); long dir = directory.EnumerateFiles("*", SearchOption.AllDirectories).Sum(fi =&gt; fi.Length); if (dir &gt;= ScanSize) Console.WriteLine(directory); } catch (UnauthorizedAccessException) { } </code></pre> <p>I should note that the input strings, and the <code>long size = Convert.ToInt32(Size)</code> come from the arguments in the <code>Main()</code></p> <p>I've read somewhere that I shouldn't use</p> <p><code>Directory.GetDirectories(ScanPath, "*", SearchOption.AllDirectories);</code></p> <p>since it'll get all the directories at once. But if I remove it, it only gets the directories in the given path, without any subdirectories. So I was told to apply recursion, but I found these fairly difficult. I read some things on <code>file.Attributes</code>, about hidden files, but I didn't know where to apply them.</p> <p>I am the administrator of the system and I plan to run this on the entire data drive. D:\ </p> <p>But in this case the error occurs when the program tries to access the trash can of the D:\ But even if it skips this specific location, the error still comes back at another inaccessible one.</p> <p>I'm hoping anyone here knows a good example or knows a website that explains this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload