简体   繁体   English

如何设置文件权限,以防止其他人对文件进行独占锁定?

[英]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. 我想使用C#/。NET打开要在Windows 7主机上追加的文件。 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. 如果我的程序未运行,则错误的Logscaner会获得排他锁,并且我程序的后续运行将无法获得跟踪文件的fd。

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. 您不能将权限设置为允许对文件的任何访问,但是由于某种原因限制了FileShare。无,因为文件共享模式与访问权限无关。

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. 有关详细信息,请参见CreateFile-dwShareMode部分

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. 在您的情况下,日志读取器需要具有打开文件的权限,因此,如果它是打开文件的第一个进程,它将能够设置ShareMode.None。 As devshorts says there is not much you can do short of changing offending process OR hacking file access methods (search for "hook CreateFile"). 正如devshorts所说,在更改有问题的过程或修改文件访问方法(搜索“挂钩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. 旁注:如果您的日志阅读器与其他进程使用同一帐户运行,则由于另一个原因,权限将无济于事-因为Windows中没有“每个进程”权限,并且所有进程将共享同一用户的权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 防止Excel导致我的应用程序因文件锁定错误而崩溃 - Prevent Excel from causing my app to crash with file lock error 如何在没有独占锁的情况下使用FileStream附加到文件? - How does one use FileStream to append to a file without an exclusive lock? 如何以编程方式从Sharepoint服务器下载文件并锁定它? - How can I programatically download a file from a sharepoint server and lock it? 如何防止计算机用户更改“安全性”选项卡中的文件权限 - How to prevent computer users from changing file permissions in the security tab 如何通过只能使用我的应用程序打开文件来锁定文件? - How can i lock a file, by making that it can only open using my application? 如何在.net中锁定文件,以便只有我的应用可以访问它? - How can I lock a file in .net so that only my app can access it? 如何更改 Google Drive 上文件的权限以与具有链接作为查看器的任何人共享? - How to change permissions of a file on Google Drive to share it with anyone with link as viewer? 我的 .NET 应用程序如何在我的应用程序读取文件时阻止其他进程写入文件? - how can my .NET app prevent other processes from writing to a file while my app reads it? 如何通过FileStream写入文件时锁定文件? - How can I lock a file while writing to it via a FileStream? 如何通过从 aspx.cs 文件中获取参数来执行数据库 CRUD 操作? - How can i do database CRUD operations by taking parameters from aspx.cs file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM