use*_*411 6 .net c# vb.net file-io
我想建立自己的平面文件数据库.以下是我访问平面文件数据库的方法
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)
Run Code Online (Sandbox Code Playgroud)
有没有通过.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 Sub
End Class
Run Code Online (Sandbox Code Playgroud)