Recursively search directories in C #

I need to move directories recursively in C #. I am doing something like this . But it throws an exception when iterating through system folders. How to check it before excluding?

+2


source to share


3 answers


You will need to check permissions, but I would highly recommend throwing exceptions instead, which makes the code clearer and will also deal with other issues, like when you want to parse directories remotely and the network stops working ...

If you are building a GUI for a directory tree, you can also add some nice lock icons in places where you get error permissions and error icons elsewhere ... C # already has a nice free open source component for this.



If you want to count files and sizes, there is no way to fix the permissions issue, you must run your tool under a more privileged user.

+8


source


I looked at your link and code. In the sample code, the try ... catch construct is used to stop the search when you enter a folder that you do not have permission to.

So, based on this, my understanding of your question can be phrased as "How can I know if I have permission to read the folder without trying to read the folder and throwing an exception if I don't have permission?"

If so, then this related post will probably be helpful to you.



Checking write permissions to directory and file in .NET

Added

I agree with @ jdehaan's answer, but only after reading the link I posted for recommendation reason.

+3


source


What kind of exception are you getting?

If you get a SecurityException you should take a look at this example on MSDN

0


source







All Articles