delete directory and file busy

S

Steph

hello, i ve a probleme when deleting a directory and when i want create
file immediatly after.


1) Directory.Delete(myPath, true);
2) TextWriter sw = new StreamWriter(myPath +"test.aspx");

i obtain
Exception Details: System.UnauthorizedAccessException: Access to the
path '***' is denied.

thanks
 
P

Patrice

Have you seen you try to create a file in a directory you just deleted ? I
would just fix this programming error (not sure if the question is why this
happens or if you are unhappy with the exception that is raised and that you
would like have another one)...
 
J

JinWeiJie

have you tried to give full control access to your app?

u may try to give full control( read , write, modify ) access to
EVERYONE.

right click the folder on your server -> share and security -> ...

may it help, thanks

Ryan Gene
 
S

Steph

ok i found !
there are a bug in Directory.Delete if you use recursive mode.
when you delete a directory recursively, re create it, and reinstall the
new file, you obtain this error :
Exception Details: System.UnauthorizedAccessException: Access to the
But, if you create your own function to delete recursively the
directory, re create it, and reinstall the new file, all is fine.

Conclusion :
dont use : Directory.Delete(xxx,true);
but :

/// <summary>
/// DirectoryDelete v1.0 by EL - 07/09/11
/// </summary>
/// <param name="pathIn"></param>
public void DirectoryDelete(string pathIn)
{
DirectoryInfo di = new DirectoryInfo(pathIn);
foreach (FileSystemInfo fsi in di.GetFileSystemInfos())
{
if ((fsi.Attributes & FileAttributes.Directory) != 0)
{
DirectoryDelete(fsi.FullName);
fsi.Delete();
}
else
{
fsi.Delete();
}
fsi.Refresh();
}
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top