p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional C# 4.0 and .NET 4 (http://p2p.wrox.com/forumdisplay.php?f=553)
-   -   Access Denied on GetFiles()method (http://p2p.wrox.com/showthread.php?t=78731)

urbnsurfr March 28th, 2010 08:26 PM

Access Denied on GetFiles()method
 
So far, I am finding this new book to be quite useful! I am having one problem I hope you can help me with. Referring to the section on file system objects, circa p. 778, you list an example program where you reference "foreach (DirectoryInfo nextFodler in theFolder.GetDirectories())".

If I attempt to use this code against my C:\users\owner\My Documents\ folder, I get an Access Denied error. If I instead poke into one of the subfolders and do directories there, I get no problems. Apparently there are system files hidden in this folder that cause GetFiles() and GetDirectories() to fail in Vista and Win7.

I have verified that I get this error in both 2008 and 2010 versions. Do you have any advice on how to work around this problem?

Kind Regards,
Dave Smith
Indianapolis, IN

ChristianNagel October 1st, 2010 12:30 PM

Where does the problem happen exactly? Accessing the directory within in the foreach loop? What file gives the problem? Did you catch the exception details?
With the my documents folder on my Windows 7 system I don't have a problem with hidden files.
Code:

            DirectoryInfo theFolder = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            foreach (DirectoryInfo nextFolder in theFolder.GetDirectories())
            {
                Console.WriteLine(nextFolder.Name);
            }


urbnsurfr October 8th, 2010 09:50 PM

It seems to be the difference in how we call the directory
 
source_dir "C:\\Users\\noz\\My Documents" string
DirectoryInfo sd = new DirectoryInfo(source_dir);

foreach (DirectoryInfo nextFolder in sd.GetDirectories())
{
Console.WriteLine(nextFolder.Name);
}
FileInfo[] files = sd.GetFiles();

In the GetDirectories() and the GetFiles() method I get the same error:
Access to the path 'C:\Users\noz\My Documents\' is denied.

However, if I instanatiate the way you made it (with the environment call):

sd = new DirectoryInfo(Environment.GetFolderPath(Environmen t.SpecialFolder.MyDocuments));


With sd instantiated this way, I do not get the access denied error. So I guess that's my way around the problem. Thanks for your help!

Kind Regards,
Dave Smith


All times are GMT -4. The time now is 04:02 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.