Jump to content

Recommended Posts

Posted

I'm new to programming with C#.

I am coding a simple VC# program which will parse a directory selected by a user

and write all the files and directories to a file.

To get the directories and files list , I'm using the directory info and File info class.

Now, the problem is for protected directories like "System Volume Information", it

throws an UnAuthorizedAccess Exception. how do i detect whether i have permission to

access the directory.


Posted (edited)

Easiest way might be to encapsulate it within a try catch block.

The main code should run in the "try" block, and if the "try" generates your error, you can use the "catch" block to handle it.

Edited by MrJinje
Posted

Easiest way might be to encapsulate it within a try catch block.

The main code should run in the "try" block, and if the "try" generates your error, you can use the "catch" block to handle it.

yes thats what i'm using now. but its not a good programming approach isn't it ?

Some other sites i came across, mentioned something called as "access control list" to retrieve the permissions

related information. but i couldn't understand how to use it.

Posted

thanks, but i was only looking for alternative to try - catch block .

In all fairness, using a try/catch block is an advanced programming technique. Trapping and knowing exactly when a specific exception will occur in ones application can in fact be the sign of a good programmer.

In this case you have an easy way to avoid the error, so yes, in some circles people might argue ICACLS is a better method. But in other circles, they might argue that ICACLS would actually slow your program if it has to first parse the ACL, before attempting the code. Whereas, the try/catch will run quicker stand-alone. (assuming you are doing thousands of folders at the same time).

It is just not a cut-and-dry situation, there are advantages to both methods. I leave the decision to you.

Posted

In all fairness, using a try/catch block is an advanced programming technique. Trapping and knowing exactly when a specific exception will occur in ones application can in fact be the sign of a good programmer.

In this case you have an easy way to avoid the error, so yes, in some circles people might argue ICACLS is a better method. But in other circles, they might argue that ICACLS would actually slow your program if it has to first parse the ACL, before attempting the code. Whereas, the try/catch will run quicker stand-alone. (assuming you are doing thousands of folders at the same time).

It is just not a cut-and-dry situation, there are advantages to both methods. I leave the decision to you.

Ok I get the deal. I was under the impression that prevention is always better than cure. It is true that I'll be iterating through

lots of directories, so an external program will indeed slow it down.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...