473,320 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

File Sharing on File.ReadAllText

I'm doing:

Dim Contents() As String = System.IO.File.ReadAllLines("C:\MyFile.txt") with
upwards of 100,000 lines.
This is a brilliantly simple and fast way of creating an array from a text
file. However, it seems to be restricted as there are no overloads for
FileSharing (I have another application which might add to the file whilst
it is being read). The only alternative I have found is using a large amount
of looping with StreamReader which takes forever.

Is there another way?

-Jerry


Sep 22 '08 #1
4 6293
On Sep 22, 10:02*am, "Jerry Spence1" <1...@dfgh.comwrote:
I'm doing:

Dim Contents() As String = System.IO.File.ReadAllLines("C:\MyFile.txt")with
upwards of 100,000 lines.
This is a brilliantly simple and fast way of creating an array from a text
file. However, it seems to be restricted as there are no overloads for
FileSharing (I have another application which might add to the file whilst
it is being read). The only alternative I have found is using a large amount
of looping with StreamReader which takes forever.

Is there another way?

-Jerry
Look at this thread to consider shared access explicty:

http://groups.google.com/group/micro...190e9b741688bb

HTH,

Onur Güzel
Sep 22 '08 #2
On Sep 22, 2:02 am, "Jerry Spence1" <1...@dfgh.comwrote:
I'm doing:

Dim Contents() As String = System.IO.File.ReadAllLines("C:\MyFile.txt") with
upwards of 100,000 lines.
This is a brilliantly simple and fast way of creating an array from a text
file. However, it seems to be restricted as there are no overloads for
FileSharing (I have another application which might add to the file whilst
it is being read). The only alternative I have found is using a large amount
of looping with StreamReader which takes forever.

Is there another way?

-Jerry
I'm not sure why you think a StreamReader would any slower than
calling ReadAllLines. Here is the code for the ReadAllLines method:

Public Shared Function ReadAllLines(ByVal path As String, ByVal
encoding As Encoding) As String()
Dim list As New ArrayList
Using reader As StreamReader = New StreamReader(path, encoding)
Dim str As String
Do While (Not str = reader.ReadLine Is Nothing)
list.Add(str)
Loop
End Using
Return DirectCast(list.ToArray(GetType(String)), String())
End Function

As you can see, it is using a StreamReader.

Chris
Sep 22 '08 #3

"Chris Dunaway" <du******@gmail.comwrote in message
news:60**********************************@m73g2000 hsh.googlegroups.com...
On Sep 22, 2:02 am, "Jerry Spence1" <1...@dfgh.comwrote:
>I'm doing:

Dim Contents() As String = System.IO.File.ReadAllLines("C:\MyFile.txt")
with
upwards of 100,000 lines.
This is a brilliantly simple and fast way of creating an array from a
text
file. However, it seems to be restricted as there are no overloads for
FileSharing (I have another application which might add to the file
whilst
it is being read). The only alternative I have found is using a large
amount
of looping with StreamReader which takes forever.

Is there another way?

-Jerry

I'm not sure why you think a StreamReader would any slower than
calling ReadAllLines. Here is the code for the ReadAllLines method:

Public Shared Function ReadAllLines(ByVal path As String, ByVal
encoding As Encoding) As String()
Dim list As New ArrayList
Using reader As StreamReader = New StreamReader(path, encoding)
Dim str As String
Do While (Not str = reader.ReadLine Is Nothing)
list.Add(str)
Loop
End Using
Return DirectCast(list.ToArray(GetType(String)), String())
End Function

As you can see, it is using a StreamReader.

Chris
Thanks Chris. I believed it would be slower because you are looping round
the Do While loop, and the System.File.ReadAllLines happens in one line - no
looping. However, I've just experimented with 1million lines and there
doesn't seem to be that much difference, so I guess this is what
System.File.ReadAllLines does behind the scenes. However, I can't find a way
of setting the FileShare using StreamReader which is what I need.

-Jerry
Sep 22 '08 #4
On Sep 22, 5:27 pm, "Jerry Spence1" <1...@dfgh.comwrote:
System.File.ReadAllLines does behind the scenes. However, I can't find a way
of setting the FileShare using StreamReader which is what I need.
Just use a FileStream to open the file and use a streamreader to read
it:

Using fs As New FileStream("filename.txt", FileMode.Append,
FileAccess.ReadWrite, FileShare.ReadWrite)
Using rdr As New StreamReader(fs)
'User streamreader here...
End Using
End Using

Chris
Sep 23 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: varlagas | last post by:
We disabled the antivirus software but the problem persists. Any clues? Many thanks in advance! Panagiotis Varlagas ======================================================================= ...
5
by: Jeff | last post by:
Hi - I'm creating an installation program that will install a VB.NET program and an instance of MSDE on an unknown desktop (commercial application). The install will include the MSDE...
17
by: J.S. | last post by:
I have a text file with parameters like the following embedded in the text: @@TextBox1@@, @@TextBox2@@, etc. I know how to read this text file. However, I am trying to figure out how to...
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
1
by: Alan T | last post by:
What will be the maximum characters the string can hold? string fileText; fileText = File.ReadAllText("c:\temp"myText.txt"); If my file size is about 30 KB, can the fileText store all the text?
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
6
by: kimiraikkonen | last post by:
Hi, I want to save all the item content of a listbox line by line into a simple text file then recall them when my project is opened. For example listbox1 contains: That - item1 Group ...
9
by: Clinto | last post by:
Hi, I am trying to find the fastest way to search a txt file for a particular string and return the line that contains the string. I have so for just used the most basic method. Initialized a...
2
by: amollokhande1 | last post by:
Hi, I am trying to read the file contents using System.IO.File.ReadAllText After reading the file contents the special charaters (á) gets replaced with some garbage characters like "?". To...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.