我如何可以锁定文件,同时通过一个FileStream写呢? [英] How can I lock a file while writing to it via a FileStream?

查看:110
本文介绍了我如何可以锁定文件,同时通过一个FileStream写呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何写一个二进制文件中的的FileStream 的BinaryWriter ,并保持文件锁定读,而我写这封信。我特别不希望其他应用程序/流程,能够同时其写入到从读取时

I am trying to figure out how to write a binary file with a FileStream and BinaryWriter, and keep the file locked for read while I am writing. I specifically don't want other applications/processes to be able to read from the while while its being written to.

//code to declare ba as a byte array

//dpath is the path to the file

FileStream BinaryFile = new FileStream(dpath, FileMode.Create, FileAccess.Write);

BinaryWriter Writer = new BinaryWriter(BinaryFile);

Writer.Write(ba);

Writer.Close();

BinaryFile.Dispose();

现在的问题是该文件可以被其他应用程序的写入,这是不可取的过程中打开我的当前的应用程序。在的FileStream 有锁的方法,但锁的写作,而不是阅读,这样就不会帮我。

Now the problem is the file can be opened by other applications during the write, which is undesirable in my current application. The FileStream has a Lock Method, but that locks for writing and not for reading, so that doesn't help me.

推荐答案

您正在寻找的的FileStream 构造的第四个参数。

You're looking for the fourth parameter of the FileStream Constructor.

public FileStream(
    string path,
    FileMode mode,
    FileAccess access,
    FileShare share
)

所以你的情况:

FileStream BinaryFile = new FileStream(dpath, FileMode.Create,
                                       FileAccess.Write, FileShare.None);

文件共享 -Enum:

包含控制的那种常数。访问其他的FileStream
的对象可以有相同的文件

Contains constants for controlling the kind of access other FileStream objects can have to the same file.

成员:


  • ,谢绝当前文件的共享。直到文件关闭打开的文件(该进程或其他进程)的任何请求都将失败。

  • ,允许该文件随后打开阅读。如果没有指定该标志,直到文件关闭打开的文件进行读取(通过这一过程或其他进程)的任何请求都将失败。然而,即使是指定了这个标志,可能仍需要额外的权限来访问文件。

  • ,允许写入该文件随后打开。如果没有指定该标志,直到文件关闭打开的文件写入(该进程或其他进程)的任何请求都将失败。然而,即使是指定了这个标志,可能还需要额外的权限来访问文件。

  • 读写,允许读取或写入该文件随后打开。如果没有指定该标志,直到文件被关闭到打开文件进行读取或写入(该进程或其他进程)的任何请求都将失败。然而,即使是指定了这个标志,可能还需要额外的权限来访问文件。

  • 删除,允许一个文件随后删除。

  • 可继承,使得文件句柄由子进程继承。此不直接被Win32支持。

  • None, Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed.
  • Read, Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
  • Write, Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
  • ReadWrite, Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for reading or writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.
  • Delete, Allows subsequent deleting of a file.
  • Inheritable, Makes the file handle inheritable by child processes. This is not directly supported by Win32.

这篇关于我如何可以锁定文件,同时通过一个FileStream写呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆