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

前端之家收集整理的这篇文章主要介绍了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同时访问单个文件

@H_502_8@

解决方法

我不知道.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
@H_502_8@ @H_502_8@ 原文链接:https://www.f2er.com/csharp/91926.html

猜你在找的C#相关文章