Click here to Skip to main content
16,022,418 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
In our application a folder is created for a project.
When I try to delete that folder in "Windows Explorer" it's allowing me to delete it.

What I want to happen is that an error occurs analogous to "Can't delete file. File is being used by another process", when i try to delete that folder.

Thank you!
Posted
Updated 2-Dec-10 23:34pm
v3
Comments
Manfred Rudolf Bihy 3-Dec-10 5:33am    
Edited for clarity and spelling.
@Subhash: I hope I put it the way you meant it. If not please feel free to elaborate.

The only way to prevent the user from deleting a folder is by having a file in that folder opened by a process (I think setting a file in the folder to be read only may also work).

Caveat: When you delete a folder, the file system first tries to delete files in the folder, and when all of them are deleted, it tries to delete the folder. It deletes files in the order they were written to the disk, so it might delete other files BEFORE it gets to the file that will prevent the file system from deleting the folder.

Example added (ManfredRBihy):
C#
StreamWriter sw = null;
StreamWriter firstSW = null;
for (int idx = 0; idx < 10; idx++)
{
    String fileName = String.Format("C:\\temp\\Manfred\\subfolder\\{0}.txt", idx);
    sw = new StreamWriter(File.Open(fileName, FileMode.Create));
    sw.Write(fileName);
    if (idx == 0)
        firstSW = sw;
    else
    {
        sw.Close();
        sw.Dispose();
    }
}
Console.WriteLine("Writing all files finished");
Console.WriteLine("Deletion of folder will fail. Hit enter!");
Console.ReadLine();
firstSW.Close();
firstSW.Dispose();
Console.WriteLine("Deletion of folder will now succeed! Hit enter");
Console.ReadLine();

End of Example
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 3-Dec-10 7:36am    
I agree, especially the part some other files first might be deleted. Are you sure about the deletion order being in the same order as the file were written to disk? If it were that way he could just write a dummy file right after folder creation and keep it open as long as the application is running.
Manfred Rudolf Bihy 3-Dec-10 7:47am    
Thanks for that tip about the deletion order. I just tried it out and was able to block deletion by creating and writing to ten files but keeping only the first StreamWriter open. I think that should pretty much solve Subhash's requirement. Thanks for that valuable hint.
johannesnestler 3-Dec-10 8:01am    
before doing that hack, I'd consider the possibilites of the OS to prevent folder deletion (security settings)
subhash04573 3-Dec-10 8:22am    
Srry, for a file i need.
not for a folder
Manfred Rudolf Bihy 3-Dec-10 8:38am    
@Subhash:I asked you to check if my translation of your request conveys what you were trying to say. If it isn't please feel free to edit your question.
just verify .., That Floder is begin used by your application.
or your application closed Properly.., Close your Application Properly and try to delete..,

Make sure that any other applications are not using this floder..,

Thanks
 
Share this answer
 
Comments
subhash04573 3-Dec-10 8:21am    
Srry, i didn't mention clearly.
it's for file only. not for folder

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900