开发者

C# - Blocking a folder from being changed while processing

开发者 https://www.devze.com 2023-02-04 14:29 出处:网络
I have a single-threaded program that processes folders and files in a sourc开发者_StackOverflow社区e directory.Is there a way to block a folder, with files in it, within my source directory from bein

I have a single-threaded program that processes folders and files in a sourc开发者_StackOverflow社区e directory. Is there a way to block a folder, with files in it, within my source directory from being modified by other processes while my program is working on it? I'm thinking something along the lines of placing some kind of exclusive lock on the folder itself, so only my program's process can use it.

NOTE:

I do not want to block the root source directory itself, just whatever folder(s), in the top level of that directory, I might be processing at any particular moment. I still want to be able to allow outside processes to add folders to the source directory, while I'm processing other folders.

UPDATE:

@Yuri - Yes this is a Windows program, a Windows Service application to be exact.

Part of what makes this both challenging and necessary is that I need to recreate the structure of whatever folder(s) I'm processing, in the source directory, in a separate destination directory. So I can't have a any other process modifying the folder(s) and File(s) while my program is working with them.


Ways to lock a folder:

1.Mark it readonly. (Included because people always try this)
Con - This does not actually work. On folders, the readonly flag is used for other purposes.

2.Mark all of the contents readonly.
Con - Won't prevent certain types of actions (e.g., creating new items).

3.Use ACL
Con - Won't prevent administrators from messing with the folder. Requires certain types of permissions.

4.Use ACL with a specially created user and enable folder Encryption.
Con - This is really of horrifying. If anything goes wrong, your data might be lost.

5.Rename/Move the folder
Con - Can be bypassed by user stupidity.

6.(Edit) gjvdkamp's answer: Lock Individual files
Con - As with #2, this still allows creating new files within the folders. That said, this is The Right Way™ to do it.


edit don't us this hack, doesn't work: http://itknowledgeexchange.techtarget.com/itanswers/folder-lock/

Else i would loop over all files in foder, create a list<FileStream> and then call Lock on each. This would give you finer grained control over wich files to lock.

GJ

Edit: Try something along these lines:

var locks = new List<FileStream>();
var di = new DirectoryInfo(@"C:\Test");
foreach (var file in di.GetFiles()) {
    var fs = new FileStream(file.FullName, FileMode.Open);
    fs.Lock(0, 0);
    locks.Add(fs);
}


Why don't you take a new copy of the folder, work on that one then replace the old one if neccessary?


I guess this might help you http://www.codeproject.com/KB/files/Unique_Folder_Protection.aspx

What the article basically does is change the extension of the folder to one of the following

".{2559a1f2-21d7-11d4-bdaf-00c04f60b9f0}" ".{21EC2020-3AEA-1069-A2DD-08002B30309D}" ".{2559a1f4-21d7-11d4-bdaf-00c04f60b9f0}" ".{645FF040-5081-101B-9F08-00AA002F954E}" ".{2559a1f1-21d7-11d4-bdaf-00c04f60b9f0}" ".{7007ACC7-3202-11D1-AAD2-00805FC1270E}"

And stores the password inside the folder

Hope this helps

Alex

0

精彩评论

暂无评论...
验证码 换一张
取 消