Directory.Delete causes access denied exception

S

SteveV

We have approximately 100 users setup to use AD roaming profiles and
offline files. Users can login to any of 50 or so desktops and have
access to their document, desktop setting, etc regardless of which
machine they log in to.

This works very well for our environment but after several months the
desktop machines start getting very slow during login. The fix is to
delete the local users on each of the desktops, which, while easy to
do manually, requires that we touch each machine.

The first step to automate the process was to put together a
WindowsForms app that calls Directory.GetDirectories on the Documents
and Settings folder, and walks through the returned string[] of folder
names and calls Directory.Delete(foldername, true) on each user
folder. The app is smart enough to not try to delete the folder of
user that is currently logged in, the "All Users", "Default User"
folders etc.

I'm running the app on the local machine and I am logged in as a
domain admin but get Access denied exceptions that look like:

Access to path 'c:documents and settings\jsmith\Application Data' is
denied
Access to path 'c:documents and settings\jsmith\Desktop' is denied
Access to path 'c:documents and settings\jsmith\Favorites' is denied
etc.

Oddly, I can manually delete the folders without error. I have
checked my assembly using the ".NET Framework 2.0 Configuration-->My
Computer-->Runtime Security Policy" which lists the permissions
granted to the assembly as "Unrestricted".

Here's an abreviated code snippet:

private void RemoveUserFolders()
{
string[] userFolders = Directory.GetDirectories
(DocsAndSettingsPath);
int folderCount = userFolders.Length;

Console.WriteLine(" Found {0} folders", folderCount);

foreach (string folder in userFolders)
{
try
{
if (IsSpecialFolder(folder))
Console.WriteLine(" Skipping reserved folder: {0}",
folder);

else
{
Console.WriteLine("Deleting user folder: {0}",
folder);
DirectoryInfo dirInfo = new DirectoryInfo(folder);
dirInfo.Delete(true);
}

}

catch (Exception ex)
{
Console.WriteLine(" An exception occurred while deleting
folder [{0}]. Reason: {1}", folder, ex.Message);
}
}
}

Any thoughts as to what might be happening?
 
G

Gregory A. Beamer

(e-mail address removed):

Any thoughts as to what might be happening?


Just a wild guess, but iterating the folders probably has a handle on the
folder you are attempting to delete.

It might be better to do a search and then delete from a list than try
deleting while you are iterating through the folders. Either that or figure
out how to get the folder from the loop rather than create a directoryinfo
separately from the name.

Just a first glance idea, so it is not well thought out. ;-)

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 
S

SteveV

(e-mail address removed):

Just a wild guess, but iterating the folders probably has a handle on the
folder you are attempting to delete.

It might be better to do a search and then delete from a list than try
deleting while you are iterating through the folders. Either that or figure
out how to get the folder from the loop rather than create a directoryinfo
separately from the name.


Hi Gregory,

Thanks for the reply. I'm getting a string[] of folder names for the
users folders so there really shouldn't be any open handles. The one
and only time I create a directory object on the folder is when I try
to delete it. I have also tried the static Directory.Delete(folder,
true) method and the result is the same.

What's really puzzling is that after I catch the exception in my app I
can still manually delete the folders in question from explorer and
don't get any errors. Also, the majority of the user's subfolders are
being deleted without error--it's only a handful of folders that cause
the exception. And when I inspect the contents of those folder
they're empty. So it looks like the call to dirInfo.Delete(true) has
successfully removed the folder contents but is choking when trying to
remove the folder. Very annoying.

Steve
 
G

Gregory A. Beamer

(e-mail address removed):

Just a wild guess, but iterating the folders probably has a handle on
the folder you are attempting to delete.

It might be better to do a search and then delete from a list than
try deleting while you are iterating through the folders. Either that
or figu re
out how to get the folder from the loop rather than create a
directoryinf o
separately from the name.


Hi Gregory,

Thanks for the reply. I'm getting a string[] of folder names for the
users folders so there really shouldn't be any open handles. The one
and only time I create a directory object on the folder is when I try
to delete it. I have also tried the static Directory.Delete(folder,
true) method and the result is the same.

What's really puzzling is that after I catch the exception in my app I
can still manually delete the folders in question from explorer and
don't get any errors. Also, the majority of the user's subfolders are
being deleted without error--it's only a handful of folders that cause
the exception. And when I inspect the contents of those folder
they're empty. So it looks like the call to dirInfo.Delete(true) has
successfully removed the folder contents but is choking when trying to
remove the folder. Very annoying.


I wonder if this holds a reference (probably not):

foreach (string folder in userFolders)

Other idea. Are the folders empty?

peace and grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top