Wrox Programmer Forums
BOOK: Professional C# 4.0 and .NET 4
This is the forum to discuss the Wrox book Professional C# 4.0 and .NET 4 by Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner; ISBN: 9780470502259
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 4.0 and .NET 4 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 28th, 2010, 08:26 PM
Registered User
Join Date: Mar 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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
Old October 1st, 2010, 12:30 PM
Wrox Author
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts

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.
            DirectoryInfo theFolder = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            foreach (DirectoryInfo nextFolder in theFolder.GetDirectories())
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
urbnsurfr (October 8th, 2010)
Old October 8th, 2010, 09:50 PM
Registered User
Join Date: Mar 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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())
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
401.3 Access denied due to Access Control List cforsyth .NET Framework 2.0 8 May 28th, 2009 01:56 PM
Access Denied for Download File(s) access. Kramerj BOOK: Expert One-on-One Access Application Development 0 May 27th, 2009 12:30 PM
GetFiles() Help adamcherochak ASP.NET 1.x and 2.0 Application Design 12 April 19th, 2008 07:21 AM
Access is denied. Ramakrishna General .NET 4 November 30th, 2004 09:05 AM
Access Denied tys MySQL 0 March 25th, 2004 03:46 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.