msdnlover

Hi All,

I am running a c# console application using framework 2.0 on win2k server. This application deletes some directories from a given server. It first loop through the files and deletes them which that directory may have and then finally delete the directory itself. The code works fine except that in some case the directory/folder still shows up in windows explorer even though the directory and all the files in it were successfully deleted. When I try to access this directory via windows explorer or through some code it says "access is denied". It looks like OS is not able to update (the status of the directory that it was deleted and no longer exists) wherever it supposed to, for whatever reason. Anybody has any clue what might be happening and what to do to resolve this



Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

IsshouFuuraibou

Was the explorer window open when you performed you delete function The explorer window is a bit sticky with the view, it doesn't constantly update the folder list. New folders and deleted folders (not deleted in the explorer window) do not get registered in the folder list until it has been updated.





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

msdnlover

I agree with you on explorer not updating its view intantly. however here the issue is different. The code deleted the directory and it doesn't exist anymore. but whenever you launch the explorer, somehow it still shows that directory. Any attempt to access that directory either through the Explorer or through the code fails. The application loops through all the directories on the server to check if there is anything for it to delete (based on a criteria). It still detects one directory which it deleted already and throws an exception as it is not able to access it anymore. Since the app runs everyday, it fails and throws an exception as soon as it try to access it.

Somehow Win2k OS didn't update the info about this directory wherever it is supposed to. I was able to reproduce it on a different win2k server. Restarting the server fixed the issue but the problem is how often you can restart Not sure what to do....anyone has a clue





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

IsshouFuuraibou

Are you doing the deleting files and subdirectories manually

You could switch to code like

Code Snippet

System.IO.DirectoryInfo di = new DirectoryInfo("C:\\test");
di.Delete(true);



The other thing that might help is refreshing the parent directory,

Code Snippet

System.IO.DirectoryInfo di = new DirectoryInfo("C:\\test");
System.IO.DirectoryInfo parent = di.Parent;
di.Delete(true);
parent.Refresh(); // Causes some updates, I know this updates the attributes



I'm not sure how much this would help, if any. It may be a bug to report to the framework team. It's acting like it's being flagged for removal but for whatever reason isn't removed until the restart.





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

msdnlover

Thanks Eric. No, I am not deleting directories manually. This is done by C# console application which runs in task scheduler everyday.

Here is code from the application.

string[] sTemp = Directory.GetFiles("Path To the Dir");

foreach (string sFile in sTemp)

{

File.Delete(sFile);

}

Directory.Delete(sDir);

Wondering how could I report it to framework team assuming it might be a bug





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

msdnlover

Thanks Eric. No, I am not deleting directories manually. This is done by C# console application which runs in task scheduler everyday.

Here is code from the application.

string[] sTemp = Directory.GetFiles("Path To the Dir");

foreach (string sFile in sTemp)

{

File.Delete(sFile);

}

Directory.Delete(sDir);

Wondering how could I report it to framework team assuming it might be a bug





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

IsshouFuuraibou

Well, that method is more manual then letting the framework delete the internals.

Directory.Delete(sDir, true); // Recursive delete, delete the files and subdirectories, then deletes the directory

should also do exactually what you've done there

There should be something where you basically force the system to update the drive and perform any pending actions. There might be something with DeviceIoControl, but I am not sure.

Also, if you want to make a bug submission for this, the proper place to do it is in Microsoft Connect under the visual studio and .net framework connection.





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

Cameron D

Try this:

Delete Folder

System.IO.Directory.Delete("C:/folder");

Cameron.






Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

nobugz

This is quite normal behavior for Windows. A directory doesn't get deleted until all handles to that directory are closed. You probably have a program running whose current working directory is that same directory. It might be Explorer or your own program, hard to tell. Given that it is a server, it might be a program running on another PC. Just don't worry about it, you can be sure that the directory will eventually disappear.





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

msdnlover

Thanks NobugZ. I understand that it'll disappear eventually but the problem is that my automated process pick this directory for deletion again next day and throws an exception. Because of this exception it cant proceed further to delete other directories which qualifies for deletion. Not sure what to do I am planning to use shell() to execute the DOS command instead of using the framework's delete funtion. Any other suggestion guys thanks a lot for your opninon and suggestions.





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

nobugz

Using the shell to execute a DOS command isn't going to fix the problem. You simply need to catch the System.IO.IOException you'll get and move on, assuming that the directory was deleted.





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

msdnlover

The idea behind executing the shell was, that may be framework is not notifying/updating the OS about the directory's status and DOS command probably will do it for sure. It may not work like you stated. The other idea i.e. catching exception and move on....sounds good to me and for this to work I wanted to check the size of the directory and if it is zero then skip it. but I am not able to find an appropriate method to check the size of the Directory either...



Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

nobugz

A directory doesn't have a size. The closest equivalent is the number of files it contains: Directory.GetFiles().Length. You should get an exception when you try this on a directory that's marked for deletion.

Generally, you'll need to make your app resilient to exceptions. You'll get other ones when you try to delete a file that is use by an application. Which in turn prevents you from deleting the directory. Not much else you can do than trying again later.





Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

Cameron D

Code Snippet

try

{

System.IO.Directory.Delete("C:/folder");

}

catch (Exception ex)

{

Console.WriteLine("Error");

Console.WriteLine(ex.Message);

}






Re: Visual C# Express Edition Weird Problem in Directory.Delete() function.

nobugz

Be sure to only catch the exceptions that you can handle. You cannot handle a OutOfMemory, StackOverflow or ExecutionEngineException. There are others. Check this thread for reference.