Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to generate list of files in specific path but whenever i am going to generate list of files I am getting error that "Access to the path xyz is denied."
but I want to skip this error and continue generating list of files.

So how I can archive this task.
Posted

Basically, you can't.
If access is denied to a folder, the "AllDirectories" search will terminate and there is no way to "restart" it.
You only option is to fetch the files in a directory manually, then recursively scan all it's subdirectories using a try-catch block to find access problems and ignore those.
 
Share this answer
 
Comments
vishal2592 22-Aug-15 5:33am    
is it possible without recursive function?
OriginalGriff 22-Aug-15 5:38am    
Yes...but it's not simple - the directory structure is naturally recursive, so you'd have to "emulate" that yourself - recursion is a simpler solution here.

Certainly, there is no "built in" function which will do it for you.
Afzaal Ahmad Zeeshan 22-Aug-15 6:43am    
The actual answer was only the first line, access is denied. No matter how much he try, .NET would always return this exception and will continuously say, "Access is denied".

(To OP) Now if you want to piss your users off with an iteration of error pop ups, then go ahead set it to a loop!

Only solution is to ask the user to grant the permission to read and write, by the way which directory you want to write the files or lists to?
OriginalGriff 22-Aug-15 6:54am    
Not necessarily.
If he's trying to do this on the root folder of a disk...do you really want the user granting "full access" to the Windows folder?
Afzaal Ahmad Zeeshan 22-Aug-15 6:56am    
That depends on user, whether he trusts the developer or not. Simple as that, if user doesn't trust he won't. If he does (most specially if he himself is the user at the moment) then he will so that application continues to function, properly. :-)
C#
foreach (var file in files)
{
    try
    {
        // your code here
    {
    catch
    {
    }
}
 
Share this answer
 
Comments
vishal2592 22-Aug-15 5:22am    
No this is not working.
My code is:
foreach (string f in Directory.GetFiles(path,"*.*",SearchOption.AllDirectories))
F-ES Sitecore 22-Aug-15 5:30am    
Rather than searching all directories in one go you're going to have to search recursively, directory by directory, so you can skip the ones you don't have access to.

https://support.microsoft.com/en-us/kb/303974
vishal2592 22-Aug-15 5:33am    
is it possible without recursive function?
F-ES Sitecore 22-Aug-15 5:54am    
No.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900