本文介绍了如何在ShareDenyWrite模式下打开StreamReader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开 StreamReader FILE_SHARE_READ,,FILE_SHARE_DELETE ?

How do i open a StreamReader with FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_DELETE?

如何打开StreamReader,以便我可以读取带有共享选项的编码文本文件,以便另一个进程可以读取该文件?

How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can read the file?

如何打开StreamReader,以便我可以阅读带有共享选项的编码文本文件,以便其他进程可以在读取文件时修改该文件?

How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can modify the file while i'm reading it?

如何打开StreamReader,以便我可以阅读带有共享选项的编码文本文件,以便其他进程可以在读取文件时删除该文件?

How do i open a StreamReader so that i can read an encoded text file, with sharing options so that another process can delete the file while i'm reading it?

.NET Framework类库中有一个名为StreamReader的类.它是唯一设计用于读取文本" 的类,这就是为什么它源自抽象基类TextReader的原因. TextReader/StreamReader允许您指定尝试打开的文件所使用的编码,并且可以为您解码文件,并返回文本的Strings.

In the .NET Framework class library there is a class called StreamReader. It is the only class designed to read "text", which is why it descends from the abstract base TextReader class. The TextReader/StreamReader allows you to specify the encoding used by the file you are trying to open, and can decode the file for you, returning Strings of text.

一旦我用StreamReader打开文件:

var sr = new StreamReader(path);

文件已被锁定,其他进程无法修改删除.我需要的是 FileStream 类的 FileShare 枚举:

The file is locked, with other processes unable to modify or delete the file. What i need is the equivalent of a FileStream class's FileShare enumeration:

  • :拒绝共享当前文件.在关闭文件之前,任何通过该过程或其他过程打开文件的请求都将失败.
  • 读取":允许随后打开文件以进行读取.如果未指定此标志,则任何打开文件以进行读取的请求(通过此过程或其他过程)都将失败,直到该文件已关闭.但是,即使指定了此标志,访问文件仍可能需要其他权限.
  • 写入:允许随后打开文件进行写入.如果未指定此标志,则打开该文件以进行写入的任何请求(通过此过程或另一个过程)都将失败,直到关闭该文件为止.但是,即使指定了此标志,访问该文件仍可能需要其他权限.
  • ReadWrite :允许随后打开文件进行读取或写入.如果未指定此标志,则打开文件以进行读取或写入的任何请求(通过此过程或另一个过程)都将失败,直到关闭文件为止.但是,即使指定了此标志,访问文件仍可能需要其他权限.
  • 删除:允许随后删除文件.
  • 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.

除了出于明显的原因,我不能使用FileStream-必须使用StreamReader.

Except that, for obvious reasons, i cannot use a FileStream - have to use a StreamReader.

如何用FileShare.ReadWrite | FileShare.Delete打开StreamReader?

推荐答案

StreamReader 具有构造函数可以串流.因此,不要使用带有字符串路径的构造函数,而是先创建具有所需选项的FileStream,然后将该FileStream传递给StreamReader构造函数.

StreamReader has a constructor that can take a stream. So instead of using the constructor that takes a string path, first create a FileStream with the options that you want, then pass that FileStream to the StreamReader constructor.

这篇关于如何在ShareDenyWrite模式下打开StreamReader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:05