加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:21:29 所属栏目:百科 来源:网络整理
导读:我想建立自己的平面文件数据库.以下是我访问平面文件数据库的方法 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是否限制使用Syst
我想建立自己的平面文件数据库.以下是我访问平面文件数据库的方法
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 Sub

End Class

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读