使用FileStream对象 [英] Working with FileStream Object

查看:67
本文介绍了使用FileStream对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个对象,而不是

StreamReader对象,因为我需要访问一个文件(

找到内容),而外部应用程序是

更新文件。当我使用

StreamReader对象时,当我在外部应用程序写入文件时尝试读取

时,我遇到了死锁。

到目前为止,我能够创建一个FileStream对象并打开

有问题的文件(CrawlerBackup.txt)。

但是我无法编写代码这将读取内容

因为我不理解重载的

方法的语法。


FileStream objFileStream =新的FileStream

(FILENAME,FileMode.Open);


bjFileStream = File.Open(CopyToFileName,FileMode.Open,

FileAccess.Read,FileShare.Read);


有人可以帮我看文件内容吗?


BeginRead有两个重载方法都需要Byte []

缓冲区作为其第一个参数。我在这个

参数中放了什么?我希望阅读内容未通过

字节数组?

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?


推荐答案

您可以使用缓冲区来存储读取的文本。没有什么比这更好的了。


例子:

为了执行这个例子,创建一个新的C#Windows应用程序

项目。将以下内容添加到表单:按钮和RichTextBox对象。

双击按钮并完成下面的代码。运行应用程序并单击按钮将导致代码读取文件并显示在

richtextbox中。希望这会有所帮助。


private void button1_Click(object sender,System.EventArgs e)

{

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

richTextBox1.Text ="" ;; //重置richtextbox


//打开流阅读。

使用(FileStream fs = File.OpenRead(path))

{

byte [] b =新字节[1024]; //用于存储读取字节的字节缓冲区

UTF8Encoding temp = new UTF8Encoding(true);


//读取文件。

while(fs.Read(b,0,b.Length)> 0)

{

//将richtextBox与读缓冲区连接起来。

richTextBox1.Text = richTextBox1.Text + temp.GetString(b);

}

}

}


干杯,

Christian T. [MSFT]

Visual Studio更新团队


- 请不要直接回复此电子邮件。此电子邮件仅供新闻组使用

目的。

========================== ======================== =======================

此帖子按原样提供。没有保证,并且没有赋予

权利。使用包含的脚本样本须遵守指定的条款

http://www.microsoft.com/info/cpyright.htm


注意:为了整个社区的利益,所有对此的回复

消息最好直接发送到新闻组/帖子来源



============ ====================================== ============ ===========

--------------------
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>
发件人:汤姆 < da ***** @ fhlbcin.com>
主题:使用FileStream对象
日期:2003年9月3日星期三13:08:01 -0700
行:32
消息ID:< 04 **************************** @ phx.gbl>
MIME-Version: 1.0
内容类型:text / plain;
charset =" iso-8859-1"
Content-Transfer-Encoding:7bit
X-Newsreader:适用于Windows的Microsoft CDO 2000
X-MimeOLE:微软MimeOLE制作V5.50.4910.0300
线程索引:AcNyVxPnviMyaLhMSWOxnDC1sZwWhA ==
新闻组:microsoft.public.dotnet.languages.csharp
路径: cpmsftngxa06.phx.gbl
外部参照: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

我正在使用这个对象,因为与StreamReader对象相反,因为我需要访问一个文件(以找到内容)当外部应用程序更新文件时。当我使用
StreamReader对象时,当我在外部应用程序写入文件时尝试读取文件时,我遇到了死锁。

到目前为止我能够创建一个FileStream对象并打开有问题的文件(CrawlerBackup.txt)。
但我无法编写将读取内容的代码
因为我不懂语法重载
方法。

FileStream objFileStream = new FileStream
(FILENAME,FileMode.Open);

bjFileStream = File.Open(CopyToFileName,FileMode。打开,
FileAccess.Read,FileShare.Read);


有人可以帮我读取文件内容吗?

BeginRead有两个重载方法需要一个Byte []
缓冲区作为其第一个参数。我在这个
参数中放了什么?我希望阅读内容未通过
Byte数组?

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?






非常感谢。感谢您的帮助。


Tom
Many thanks. I appreciate your help.

Tom
-----原始消息-----
您可以使用缓冲区来存储读取的文本。没有什么
就像一个很好的例子。
示例:
为了执行这个例子,创建一个新的C#Windows
Applicationproject。将以下内容添加到表单:一个按钮和
RichTextBox对象。双击按钮并完成下面的代码。
运行应用程序并单击该按钮将导致代码读取文件
并将其显示在richtextbox中。希望这会有所帮助。

private void button1_Click(object sender,
System.EventArgs e){
string path = @" C:\CrawlerBackup.txt" ;;
richTextBox1.Text ="" ;; //重置richtextbox
//打开要读取的流。
使用(FileStream fs = File.OpenRead(path))
{
byte [] b =新字节[1024]; //字节缓冲区
存储读取的字节UTF8Encoding temp = new UTF8Encoding
(true);
//读取文件。
while(fs.Read(b,0, b.Length)> 0)
//将richtextBox
与读缓冲区连接起来。 richTextBox1.Text =
richTextBox1.Text + temp.GetString(b); }
}

干杯,
Christian T. [MSFT]
Visual Studio更新团队

- 请做不直接回复此电子邮件。这封电子邮件
仅适用于newsgrouppurposes。
=================================== ============== =========
===============此帖子提供按现状 ;没有保证,
赋予权利。使用包含的脚本示例需遵守 http:/指定的
条款/www.microsoft.com/info/cpyright.htm


注意:为了整个社区的利益,所有
对此消息的回复最好是针对来自
的新闻组/主题来自。来自=================================== ============== =========
===============
---- ----------------
-----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>
发件人:汤姆 < da ***** @ fhlbcin.com>
主题:使用FileStream对象
日期:2003年9月3日星期三13:08:01 -0700
行:32
消息ID:< 04 **************************** @ phx.gbl>
MIME-Version: 1.0
内容类型:text / plain;
charset =" iso-8859-1"
Content-Transfer-Encoding:7bit
X-Newsreader:适用于Windows的Microsoft CDO 2000
X-MimeOLE:微软MimeOLE制作V5.50.4910.0300
线程索引:AcNyVxPnviMyaLhMSWOxnDC1sZwWhA ==
新闻组:microsoft.public.dotnet.languages.csharp
路径: cpmsftngxa06.phx.gbl
外部参照: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

我正在使用这个对象,因为与StreamReader对象相反,因为我需要访问一个文件(以找到内容)当外部应用程序更新文件时。当我使用
StreamReader对象时,当我尝试
读取文件而我的外部应用程序写入
时,我遇到了死锁。
到目前为止,我能够创建一个FileStream对象并打开有问题的文件(CrawlerBackup.txt)。
但是我无法编写将读取
内容的代码,因为我不明白它的语法
重载方法。

FileStream objFileStream = new FileStream
(FILENAME,FileMode.Open);

bjFileStream = File.Open(CopyToFileName,FileMode。打开,
FileAccess.Read,FileShare.Read);


有人可以帮我读取文件内容吗?

BeginRead有两个重载方法需要一个Byte
[]缓冲区作为其第一个参数。我在这个
参数中放了什么?我希望阅读内容未通过
Byte数组?

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?





.



Christian


一个额外的问题


@ in
的目的是什么? >
string path = @" C:\CrawlerBackup.txt" ;;

谢谢


Tom

Christian

One additional question

What is the purpose of the @ in

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

Tom

-----原始消息-----
您可以使用缓冲区来存储读取的文本。没有什么
就像一个很好的例子。
示例:
为了执行这个例子,创建一个新的C#Windows
Applicationproject。将以下内容添加到表单:一个按钮和
RichTextBox对象。双击按钮并完成下面的代码。
运行应用程序并单击该按钮将导致代码读取文件
并将其显示在richtextbox中。希望这会有所帮助。

private void button1_Click(object sender,
System.EventArgs e){
string path = @" C:\CrawlerBackup.txt" ;;
richTextBox1.Text ="" ;; //重置richtextbox
//打开要读取的流。
使用(FileStream fs = File.OpenRead(path))
{
byte [] b =新字节[1024]; //字节缓冲区
存储读取的字节UTF8Encoding temp = new UTF8Encoding
(true);
//读取文件。
while(fs.Read(b,0, b.Length)> 0)
//将richtextBox
与读缓冲区连接起来。 richTextBox1.Text =
richTextBox1.Text + temp.GetString(b); }
}

干杯,
Christian T. [MSFT]
Visual Studio更新团队

- 请做不直接回复此电子邮件。这封电子邮件
仅适用于newsgrouppurposes。
=================================== ============== =========
===============此帖子提供按现状 ;没有保证,
赋予权利。使用包含的脚本示例需遵守 http:/指定的
条款/www.microsoft.com/info/cpyright.htm


注意:为了整个社区的利益,所有
对此消息的回复最好是针对来自
的新闻组/主题来自。来自=================================== ============== =========
===============
---- ----------------
-----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>
发件人:汤姆 < da ***** @ fhlbcin.com>
主题:使用FileStream对象
日期:2003年9月3日星期三13:08:01 -0700
行:32
消息ID:< 04 **************************** @ phx.gbl>
MIME-Version: 1.0
内容类型:text / plain;
charset =" iso-8859-1"
Content-Transfer-Encoding:7bit
X-Newsreader:适用于Windows的Microsoft CDO 2000
X-MimeOLE:微软MimeOLE制作V5.50.4910.0300
线程索引:AcNyVxPnviMyaLhMSWOxnDC1sZwWhA ==
新闻组:microsoft.public.dotnet.languages.csharp
路径: cpmsftngxa06.phx.gbl
外部参照: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

我正在使用这个对象,因为与StreamReader对象相反,因为我需要访问一个文件(以找到内容)当外部应用程序更新文件时。当我使用
StreamReader对象时,当我尝试
读取文件而我的外部应用程序写入
时,我遇到了死锁。
到目前为止,我能够创建一个FileStream对象并打开有问题的文件(CrawlerBackup.txt)。
但是我无法编写将读取
内容的代码,因为我不明白它的语法
重载方法。

FileStream objFileStream = new FileStream
(FILENAME,FileMode.Open);

bjFileStream = File.Open(CopyToFileName,FileMode。打开,
FileAccess.Read,FileShare.Read);


有人可以帮我读取文件内容吗?

BeginRead有两个重载方法需要一个Byte
[]缓冲区作为其第一个参数。我在这个
参数中放了什么?我希望阅读内容未通过
Byte数组?

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?





.



这篇关于使用FileStream对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆