delete directory and file busy

  • Thread starter Thread starter Steph
  • Start date Start date
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
 
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)...
 
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
 
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();
}
}
 
Back
Top