Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a limit for System.IO.FileShare?

I want to build my own flat file database. Here is how I access the flat file database

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)

Is there a limit imposed by .Net for the usage of System.IO.FileShare.Read, System.IO.FileShare.Write and System.IO.FileShare.ReadWrite when dealing with a file?

I mean is that .Net capable to support thousands of users using file stream and stream reader objects with System.IO.FileShare.Read to access a single file concurrently?

like image 586
user774411 Avatar asked Aug 05 '11 11:08

user774411


People also ask

What is System IO file?

File Class (System.IO)Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of FileStream objects.

What is file share enumeration?

A typical use of this enumeration is to define whether two processes can simultaneously read from the same file. For example, if a file is opened and Read is specified, other users can open the file for reading but not for writing.


3 Answers

If you try to open a file with conflicting access and share permissions it won't work. But if this is a custom database, why would you need more than one file handle open? Your custom database software should manage the open handles (having 1 per file). As for your specific question, there's no set limit, but subsequent opens of the file need to follow the rules for access and sharing permissions.

http://msdn.microsoft.com/en-us/library/aa363874%28v=vs.85%29.aspx

like image 129
LastCoder Avatar answered Oct 09 '22 18:10

LastCoder


The FileShare member means that other files can also open the file. This does not guarantee that data will be synchronised in any way - it simply means that multiple programs can now read (since that's what you set - FileShare.Read) from that file while you have it open.

If you use ReadWrite, then multiple programs can read from and write to the file. Again, you will not be notified of any changes. If multiple programs are writing to the same file at the same time as a stream, the data will be mixed together and you'll get a corrupt file. (Corrupt meaning that neither you nor the other program will be able to decompile it because your data is intertwined with your friends' application).

There are no unreasonable limitations to the number of concurrent programs reading a file.

like image 23
foxy Avatar answered Oct 09 '22 17:10

foxy


I don't know the exact limit imposed by .NET/windows, so I have created a real test for you. I ran the following test code for few minutes and I found that up to 635908 counts of system.io.fileshare usage, it still functional, i.e. you can still read the flat database file's content.

Here is the code (it is a winform application, .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
like image 2
Predator Avatar answered Oct 09 '22 17:10

Predator