By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,119 Members | 2,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,119 IT Pros & Developers. It's quick & easy.

Working with FileStream Object

P: n/a
Tom
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?


Nov 15 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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?



Nov 15 '05 #2

P: n/a
Tom
Many thanks. I appreciate your help.

Tom
-----Original Message-----
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 Applicationproject. 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 andclicking the button will cause the code to read the file and display it inthe 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 newsgrouppurposes only.
================================================= ========= ===============This posting is provided "AS IS" with no warranties, and confers norights. Use of included script samples are subject to the terms specifiedat http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to thismessage are best directed to the newsgroup/thread from whichthey 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?



.

Nov 15 '05 #3

P: n/a
Tom
Christian

One additional question

What is the purpose of the @ in

string path = @"C:\CrawlerBackup.txt";
Thanks

Tom

-----Original Message-----
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 Applicationproject. 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 andclicking the button will cause the code to read the file and display it inthe 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 newsgrouppurposes only.
================================================= ========= ===============This posting is provided "AS IS" with no warranties, and confers norights. Use of included script samples are subject to the terms specifiedat http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to thismessage are best directed to the newsgroup/thread from whichthey 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?



.

Nov 15 '05 #4

P: n/a
Tom
Dear Christian:

I'm very sorry but I have one more question...

I hope you don't mind.

Is it your understanding that using FileStream object over
the StreamReader will allow me to read a file while
another application cn be updating it at the same time?

See I went down the path of useing the StreamReader object
but found it would lock the file when I either read it or
copied it. This caused the other application (which
writes to it) to hang up.

Since I can't have this I found this object has
Asynchonronization.

Am I on the right track???
-----Original Message-----
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 Applicationproject. 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 andclicking the button will cause the code to read the file and display it inthe 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 newsgrouppurposes only.
================================================= ========= ===============This posting is provided "AS IS" with no warranties, and confers norights. Use of included script samples are subject to the terms specifiedat http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to thismessage are best directed to the newsgroup/thread from whichthey 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?



.

Nov 15 '05 #5

P: n/a

"Tom" <da*****@fhlbcin.com> wrote in message
news:05****************************@phx.gbl...
Dear Christian:

I'm very sorry but I have one more question...

I hope you don't mind.

Is it your understanding that using FileStream object over
the StreamReader will allow me to read a file while
another application cn be updating it at the same time?

See I went down the path of useing the StreamReader object
but found it would lock the file when I either read it or
copied it. This caused the other application (which
writes to it) to hang up.


FileStream just gives you more low-level control over how you open and read
the file.

Once you have opened the filestream with whatever kind of access and sharing
you want, you can create a new StreamReader to read from the FileStream.

IO.FileStream fs = new IO.FileStream("c:\foo.txt", IO.FileMode.Open,
IO.FileAccess.Read);
IO.StreamReader sr = New IO.StreamReader(fs);

Then just use the StreamReader. Best of both worlds.

David
Nov 15 '05 #6

P: n/a
Tom
Thanks David for your tip. I never thought about mixing
the objects together.

But I'm puzzled about something. Is their an advantage to
your technique and do I have to close both objects? Will
this approach work when an external application is
updating the file while I'm trying to read it?

Thanks

Tom
Also, do you know what the @ does in does??
string path = @"C:\CrawlerBackup.txt";
-----Original Message-----

"Tom" <da*****@fhlbcin.com> wrote in message
news:05****************************@phx.gbl...
Dear Christian:

I'm very sorry but I have one more question...

I hope you don't mind.

Is it your understanding that using FileStream object over the StreamReader will allow me to read a file while
another application cn be updating it at the same time?

See I went down the path of useing the StreamReader object but found it would lock the file when I either read it or copied it. This caused the other application (which
writes to it) to hang up.

FileStream just gives you more low-level control over how

you open and readthe file.

Once you have opened the filestream with whatever kind of access and sharingyou want, you can create a new StreamReader to read from the FileStream.
IO.FileStream fs = new IO.FileStream("c:\foo.txt", IO.FileMode.Open,IO.FileAccess.Read);
IO.StreamReader sr = New IO.StreamReader(fs);

Then just use the StreamReader. Best of both worlds.

David
.

Nov 15 '05 #7

P: n/a
Mixing a Stream and a Reader, you only need to close the Reader and it
closes the underlying stream. This can actually be a pain since there are
cases where you might want to use different types of readers on the same
underlying stream (read some text, then read some binary for instance), but
there was a smal oversight for this when creating the IO classes.

There is an advantage to using a Stream and a Reader. You can explicitly
call any special overloads on the stream. This option isn't available when
you just use a reader. Internally the reader simply allocates a FileStream
with the most often used set of parameters. In your case, the FileStream
was being automatically created using on the filename and whatever the
default values are for the rest of the FileStream.
--
Justin Rogers
DigiTec Web Consultants, LLC.

"Tom" <da*****@fhlbcin.com> wrote in message
news:05****************************@phx.gbl...
Thanks David for your tip. I never thought about mixing
the objects together.

But I'm puzzled about something. Is their an advantage to
your technique and do I have to close both objects? Will
this approach work when an external application is
updating the file while I'm trying to read it?

Thanks

Tom
Also, do you know what the @ does in does??
string path = @"C:\CrawlerBackup.txt";
-----Original Message-----

"Tom" <da*****@fhlbcin.com> wrote in message
news:05****************************@phx.gbl...
Dear Christian:

I'm very sorry but I have one more question...

I hope you don't mind.

Is it your understanding that using FileStream object over the StreamReader will allow me to read a file while
another application cn be updating it at the same time?

See I went down the path of useing the StreamReader object but found it would lock the file when I either read it or copied it. This caused the other application (which
writes to it) to hang up.


FileStream just gives you more low-level control over how

you open and read
the file.

Once you have opened the filestream with whatever kind of

access and sharing
you want, you can create a new StreamReader to read from

the FileStream.

IO.FileStream fs = new IO.FileStream("c:\foo.txt",

IO.FileMode.Open,
IO.FileAccess.Read);
IO.StreamReader sr = New IO.StreamReader(fs);

Then just use the StreamReader. Best of both worlds.

David
.

Nov 15 '05 #8

P: n/a
Tom
David:

While I understand about the no guarantees... I wanted to
provide more information.
The other application is simply updating itself with
progress. The other application is MondSearch and it
updates this file when an incremental or full reindex is
initiated by the end user.

They change the status from Init to Grab to Pubish to
Complete. If their is an error the message will be Error
along with an explanation.

All I'm trying to do is "read" the file and in some case
the file may be being updated while I attempt to read it.

So, I found the StreamReader object to lock the file while
I was trying to read it thus disallowing the other
application to update it, thereby locking the search
update.

I've been told by the vendor that they update the file
with read write access. So I'm hoping using the
FileStream will help solve this.

Tom
-----Original Message-----

"Tom" <da*****@fhlbcin.com> wrote in message
news:05****************************@phx.gbl...
Thanks David for your tip. I never thought about mixing
the objects together.

But I'm puzzled about something. Is their an advantage to your technique and do I have to close both objects?
Closing the StreamReader will close the underlying stream.
Will
this approach work when an external application is
updating the file while I'm trying to read it?


No guarantee here. It depends on whether the other

application is willingto share access to the file
and what sort of changes the other app is making to the file.
David
.

Nov 15 '05 #9

P: n/a
The @ sign turns off escape sequences like \n, \r, \t etc and allows you to
have line breaks in the string.

This

string str = "line one\r\nline two, \"text in quotes\"";

is equivalent to this:

string str = @"line one
line two, ""text in quotes""";

This is most useful for string like paths where you have a lot of '\'
characters
(i.e. this @"c:\dir\sub\file.txt" is nicer than this
"c:\\dir\\sub\\file.txt")

Josh
"Tom" <da*****@fhlbcin.com> wrote in message
news:06****************************@phx.gbl...
Christian

One additional question

What is the purpose of the @ in

string path = @"C:\CrawlerBackup.txt";
Thanks

Tom

-----Original Message-----
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?



.

Nov 15 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.