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?
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.
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.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With