views:

99

answers:

2

Hello everyone,

I am using VSTS 2008 + C# + .Net 3.5 to develop a console application. For file access, we can access file in exclusive mode which blocks other process/thread from access the file -- which has the effect of "lock" the file.

I am wondering for directory, are there any built-in API or solution to make the directory exclusive access -- has the effect of "locked"? For example, if one thread "locks" the directory, other thread/process can not call Directory.GetFiles, can not write file to the directory, can not read a file from the directory, etc.

thanks in advance, George

+1  A: 

I think what you can do is set the directory's Access Control and then change it back when your done. Have a look here:

http://msdn.microsoft.com/en-us/library/system.io.directory.setaccesscontrol.aspx

BFree
You mean remove access of all other users except me? If yes, my confusion is, if I create a process which has 2 threads, both threads are run on the same account, in this scenario, how can I prevent one of the thread to access, but enable the access of the other thread?
George2
+1  A: 

If it is your own process that might interfere you can use a named Mutex to synchronize. It is very hard to keep out other processes, the usual approach is to evade to a temp (hidden) dir, do your work and copy/move the results back.

Henk Holterman
1. "the usual approach is to evade to a temp (hidden) dir, do your work and copy/move the results back." -- good idea! Appreciate if you could describe more or recommend me some more readings on this topic? 2. I want to confirm with you there is no built-in file system API to lock a directory?
George2
Thanks for your help Henk, I have marked your reply as answered.
George2