问题
FileStream infile = new FileStream(@"C:\Users\John\Desktop\ProjectNew\nov.txt", FileMode.Open, FileAccess.Read);
int position = x.Length;
infile.Seek(position, SeekOrigin.Begin);
But Seek method returns number. How to read the file 'infile' from position to end in a string?
回答1:
Is this what you're after? Assuming you wanted to start reading from position 100...
using (FileStream fs = new FileStream(@"file.txt", FileMode.Open, FileAccess.Read))
{
fs.Seek(100, SeekOrigin.Begin);
byte[] b = new byte[fs.Length - 100];
fs.Read(b, 0, (int)(fs.Length - 100));
string s = System.Text.Encoding.UTF8.GetString(b);
}
回答2:
The Seek method is supposed to return a number, the new position in the stream. Now just call whatever Read function you want.
回答3:
Seek() only places the file pointer somewhere else. If you do a read before the seek, it will read from the beginning of the file. If you read after the seek, it will start reading from position.
So to read the file from position to the end, do Seek(), followed by Read() or ReadToEnd().
来源:https://stackoverflow.com/questions/7596302/read-file-from-position