You can use a buffer to store the read text. Nothing like a good example.
EXAMPLE:
In order to execute this example, Create a new C# Windows Application
project. Add the following to the Form: a button and a RichTextBox object.
Double click on the button and complete the code below. Running the app and
clicking the button will cause the code to read the file and display it in
the richtextbox.. Hope this helps.
private void button1_Click(object sender, System.EventArgs e)
{
string path = @"C:\CrawlerBackup.txt";
richTextBox1.Text = ""; // reset the richtextbox
//Open the stream for reading.
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024]; // byte buffer to store the read bytes
UTF8Encoding temp = new UTF8Encoding(true);
// read the file.
while (fs.Read(b,0,b.Length) > 0)
{
// concatenate the richtextBox with the read buffer.
richTextBox1.Text = richTextBox1.Text + temp.GetString(b);
}
}
}
Cheers,
Christian T. [MSFT]
Visual Studio Update Team
- Please do not reply to this email directly. This email is for newsgroup
purposes only.
================================================== =======================
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm
Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which
they originated.
================================================== =======================
--------------------
Content-Class: urn:content-classes:message
From: "Tom" <da*****@fhlbcin.com>
Sender: "Tom" <da*****@fhlbcin.com>
Subject: Working with FileStream Object
Date: Wed, 3 Sep 2003 13:08:01 -0700
Lines: 32
Message-ID: <04****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNyVxPnviMyaLhMSWOxnDC1sZwWhA==
Newsgroups: microsoft.public.dotnet.languages.csharp
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:182044
NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
I am working with the this object as oppose to the
StreamReader object becuase I need to access a file (to
find the contents) while an external application is
updating the file. When I was working with the
StreamReader object I got a deadlock when I tried reading
the file while my external application was writing to it.
So far I am able to create a FileStream object and open
the file in question (CrawlerBackup.txt).
But I am unable to write code that will read the contents
because I don't understand the syntax for the overloaded
methods.
FileStream objFileStream = new FileStream
(FILENAME,FileMode.Open);
bjFileStream = File.Open(CopyToFileName,FileMode.Open,
FileAccess.Read,FileShare.Read);
Can someone help me read the file contents?
BeginRead has two overloaded methods both require a Byte[]
buffer as its first argument. What do I put in this
parameter? I wish to read the contents not pass in a
Byte array?