newprouser Posted May 8, 2010 Posted May 8, 2010 I'm new to programming with C#.I am coding a simple VC# program which will parse a directory selected by a userand 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 toaccess the directory.
MrJinje Posted May 8, 2010 Posted May 8, 2010 (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 May 8, 2010 by MrJinje
newprouser Posted May 8, 2010 Author Posted May 8, 2010 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 permissionsrelated information. but i couldn't understand how to use it.
MrJinje Posted May 8, 2010 Posted May 8, 2010 (edited) In that case, why not use ICACLS.exe or CACLS.exe to read the permissions prior to running your code. Edited May 8, 2010 by MrJinje
newprouser Posted May 9, 2010 Author Posted May 9, 2010 Thanks MrJinje i will try it out.See this sample code (just click on the C# tab) thanks, but i was only looking for alternative to try - catch block .
MrJinje Posted May 9, 2010 Posted May 9, 2010 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.
newprouser Posted May 9, 2010 Author Posted May 9, 2010 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 throughlots of directories, so an external program will indeed slow it down.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now