C# · 12月 24, 2021

c# – System.IO.FileShare是否有限制?

我想建立自己的平面文件数据库.以下是我访问平面文件数据库的方法 Dim fs As New System.IO.FileStream(“C:\MyDb.txt”,IO.FileMode.Open,IO.FileAccess.Read,IO.FileShare.Read)Dim sr As New System.IO.StreamReader(fs)

在处理文件时,.Net是否限制使用System.IO.FileShare.Read,System.IO.FileShare.Write和System.IO.FileShare.ReadWrite?

我的意思是.Net能够支持成千上万的用户使用文件流和流读取器对象与System.IO.FileShare.Read同时访问单个文件?

解决方法 我不知道.NET / windows强加的确切限制,所以我为你创建了一个真正的测试.我运行了以下测试代码几分钟,我发现最多635908次使用system.io.fileshare,它仍然可用,即你仍然可以读取平面数据库文件的内容.

这是代码(它是一个winform应用程序,.Net 4):

Public Class Form1 Private Sub Button1_Click(sender As System.Object,e As System.EventArgs) Handles Button1.Click Dim filepath As String = “c:\database.txt” Dim filestream As System.IO.FileStream Dim count As Int32 For count = 0 To system.int32.MaxValue filestream = New System.IO.FileStream(filepath,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read) AppendLog(count,filestream.ReadByte) Next End Sub Private LogFilepath As String = “C:\LogInfo.txt” Private Enter As String = Chr(13) & Chr(10) Private Space As String = ” ” Private Sub AppendLog(ByVal Sequence As Int32,ByVal info As Byte) System.IO.File.AppendAllText(LogFilepath,Enter & Sequence & Space & CStr(info)) End SubEnd Class