简体   繁体   中英

How can I set file permissions to prevent anyone else from taking exclusive lock on my file?

I want to open file for append on Windows 7 host using C#/.NET. I want to use usual file output operations for this purpose. I found such solution:

FileStream trace_fd = new FileStream(r"c:\temp\testlog.txt", 
                                     FileMode.Append, 
                                     FileAccess.Write, 
                                     FileShare.ReadWrite)

My tracefile is readable even if my program with line above is up and running. I don't have exception that file is used by another process anymore from proper code like this:

FileStream good_logreader_fd = new FileStream("c:\temp\testlog.txt", 
                                              FileMode.Read, 
                                              FileAccess.Read,
                                              FileShare.ReadWrite)

Then I have another program (buggy log scanner) with such line:

FileStream bad_logreader_fd = new FileStream("c:\temp\testlog.txt", 
                                             FileMode.Read, 
                                             FileAccess.Read, 
                                             FileShare.None)

Such line is obviously a bug for log scanner. If my program is not running then buggy logscaner will get exclusive lock and subsequent runs of my program will fail to get fd for trace file.

My question is what can I do to prevent such horrific scenario from happening. Eg can I set file permissions for the tracefile to prevent anyone from taking exclusive lock? If yes then how? Anything else which can protect my program from buggy log scanner problem? Note that I have to keep the same trace filename between my program runs.

据我所知,您不能阻止其他人在不修改其代码的情况下尝试以另一种文件访问方式打开文件。

You can not set permissions to allow any access to a file, but somehow restrict FileShare.None as file sharing mode is not related to access permissions.

Permissions give a process (based on account it runs under) access to a file/resource. If access granted process can open file with whatever share mode it desires. If this share mode does not conflict with existing share modes on the file request succeeds and now file will have this share mode (combined with previous one). See CreateFile- dwShareMode section for details.

In your case log reader needs to have permissions to open file, so it will be able to set ShareMode.None if it the first process to open file. As devshorts says there is not much you can do short of changing offending process OR hacking file access methods (search for "hook CreateFile").

Side note: if your log reader runs under the same account as other processes permissions will not help for one additional reason - as there is no "per process" permissions in Windows and all processes will share the same user's permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM