简体   繁体   English

在C#中读取(/写入)文件

[英]Reading(/Writing) Files in C#

I recently wanted to track progress of a HTTPWebRequest Upload progress. 我最近想跟踪HTTPWebRequest上传进度。 So I started small and started with buffered read of a simple text file. 因此,我从小处着手,以缓冲读取一个简单的文本文件开始。 I then discovered that a simple task like 然后我发现一个简单的任务

File.ReadAllText("text.txt");

becomes something like below, with all the streams, readers, writers etc. Or can somethings be removed? 变成下面的内容,包括所有流,读者,作家等。还是可以删除某些内容? Also the code below is not working. 另外,下面的代码不起作用。 Maybe I did something wrong, whats the way to read (i guess write will be similar) into buffer so that I can track progress, assuming the stream are not local eg. 也许我做错了什么,假设流不是本地的,那么读入缓冲区的方式是什么(我猜写会类似),以便可以跟踪进度。 WebRequest Web请求

byte[] buffer = new byte[2560]; // 20KB Buffer, btw, how should I decide the buffer size?
int bytesRead = 0, read = 0;
FileStream inStream = new FileStream("./text.txt", FileMode.Open, FileAccess.Read);
MemoryStream outStream = new MemoryStream();
BinaryWriter outWriter = new BinaryWriter(outStream);

// I am getting "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."
// inStream.Length = Length = 9335092
// bytesRead = 2560
// buffer.Length = 2560
while ((read = inStream.Read(buffer, bytesRead, buffer.Length)) > 0)
{
    outWriter.Write(buffer);
    //outStream.Write(buffer, bytesRead, buffer.Length);
    bytesRead += read;
    Debug.WriteLine("Progress: " + bytesRead / inStream.Length * 100 + "%");
}
outWriter.Flush();

txtLog.Text = outStream.ToString(); 

Update: Solution 更新:解决方案

byte[] buffer = new byte[2560];
int bytesRead = 0, read = 0;
FileStream inStream = File.OpenRead("text.txt");
MemoryStream outStream = new MemoryStream();

while ((read = inStream.Read(buffer, 0, buffer.Length)) > 0)
{
    outStream.Write(buffer, 0, buffer.Length);
    bytesRead += read;
    Debug.WriteLine((double)bytesRead / inStream.Length * 100);
}

inStream.Close();
outStream.Close();

应该是

outWriter.Write(buffer,0,read);

Since you seem to be reading text (although I could be wrong), it seems that your program could be a lot simpler if you read character by character instead of calling the standard Read(): 由于您似乎正在阅读文本(尽管我可能错了),所以如果您逐个字符地读取而不是调用标准的Read(),则您的程序似乎会简单得多:

BinaryReader reader = new BinaryReader(File.Open("./text.txt", FileMode.Open));
MemoryStream outStream = new MemoryStream();
StreamWriter outWriter = new StreamWriter(outStream);

while (Reader.BaseStream.Position < Reader.BaseStream.Length)
{
    outWriter.Write(reader.ReadChar());
    Debug.WriteLine("Progress: " + ((double)reader.BaseStream.Position) / (double)(reader.BaseStream.Length) + "%");
}
outWriter.Close();
txtLog.Text = outStream.ToString();

Since you only need to check the progress of the upload operation you can just check the size of the file using a fileinfo object. 由于您只需要检查上传操作的进度,因此您可以使用fileinfo对象检查文件的大小。

In the FileInfo class theres a property called length that returns the file size in bytes. 在FileInfo类中,有一个名为length的属性,该属性返回字节的文件大小。 Not sure if it gives the current size when the file being written. 不确定在写入文件时是否给出当前大小。 But I think it'll be worth giving a try as it is more simple and efficient than the method that you are using 但是我认为值得一试,因为它比您使用的方法更简单,更有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM