Working with FileStream Object

  • Thread starter Thread starter Tom
  • Start date Start date
T

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?
 
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" <[email protected]>
Sender: "Tom" <[email protected]>
Subject: Working with FileStream Object
Date: Wed, 3 Sep 2003 13:08:01 -0700
Lines: 32
Message-ID: <[email protected]>
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?
 
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 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" <[email protected]>
Sender: "Tom" <[email protected]>
Subject: Working with FileStream Object
Date: Wed, 3 Sep 2003 13:08:01 -0700
Lines: 32
Message-ID: <[email protected]>
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?

.
 
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" <[email protected]>
Sender: "Tom" <[email protected]>
Subject: Working with FileStream Object
Date: Wed, 3 Sep 2003 13:08:01 -0700
Lines: 32
Message-ID: <[email protected]>
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?

.
 
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 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" <[email protected]>
Sender: "Tom" <[email protected]>
Subject: Working with FileStream Object
Date: Wed, 3 Sep 2003 13:08:01 -0700
Lines: 32
Message-ID: <[email protected]>
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?

.
 
Tom said:
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
 
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";
 
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.
 
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
 
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 said:
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" <[email protected]>
Sender: "Tom" <[email protected]>
Subject: Working with FileStream Object
Date: Wed, 3 Sep 2003 13:08:01 -0700
Lines: 32
Message-ID: <[email protected]>
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?

.
 
Back
Top