-2

How do I see if a directory is actually open or not in c#?

Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
Donis
  • 5
  • 2
  • 12
    Please define "open"? – user703016 Mar 04 '12 at 17:05
  • Are you asking how to detect if a folder is in use? – moribvndvs Mar 04 '12 at 17:06
  • With "open" i mean that before i start the program i open the directory, then i open the program and i want that my program see if the directory is open or not and if i close the directory, the program must say me that the directory isn't open... – Donis Mar 04 '12 at 23:15
  • Welcome to Stack Overflow. Please read [How to Ask](http://stackoverflow.com/questions/how-to-ask), [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/), and [How To Ask Questions The Smart Way](http://catb.org/esr/faqs/smart-questions.html). –  Mar 05 '12 at 13:45

2 Answers2

1

You didn't respond for my comment, but if you need it because of locking case so read this simliar questions:

How to identify whether folder is opened?

and those 2:

How do I find out which process is locking a file using .NET?

Using C#, how does one figure out what process locked a file?

you can use CheckAccess as:

bool CheckAccess 
{
    try 
    {
       // If no access it will throw an exception
       Directory.Move("old","new");
       return true;
    } 
    catch ( IOException ) 
    {
       return false;
    }
}
Community
  • 1
  • 1
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
  • thanks, sorry but my internet connection was "out of oreder" :) – Donis Mar 04 '12 at 21:10
  • I need it because I want, when my program start, to create a directory with my program that contains some exe, then i want to let the user work with this exe and finally delete the directory when the user closes it(the directory).. sorry for my english :) – Donis Mar 04 '12 at 21:16
  • In the first link there would be what I search but the CheckAccess(folder) method dosen't work... i need to add in my references some using? – Donis Mar 04 '12 at 22:49
  • The problem is that Directory.Move("old","new") is always executed even if the directory is currently open :( – Donis Mar 05 '12 at 09:28
  • I'm not familiar with any event that popup up when you close a directory, i don't think it's possible in simple c# – Dor Cohen Mar 05 '12 at 11:22
0

I presume this is because you're trying to delete the folder, but a file inside is inside it is in use.

I'd do this by getting all the files in the folder, then iterating through them to see if any of them have locks.

This will give you ways to get the files in a folder, and this SO question will give you ways to see if the file is in use.

Community
  • 1
  • 1
Bridge
  • 29,818
  • 9
  • 60
  • 82