Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#中的StreamReader从特定位置读取文件?_C# - Fatal编程技术网

如何使用C#中的StreamReader从特定位置读取文件?

如何使用C#中的StreamReader从特定位置读取文件?,c#,C#,我有一个文本文件,我想从一个特定的文本文件一直读到文件的末尾 我可以用下面的方法来做 string path = @"C:\abc.txt"; var pp = File.ReadAllLines(path).Skip(1).Take(2); 但我只希望使用StreamReader完成此操作 我正在这样做,但没有给出正确的结果 using (Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Rea

我有一个文本文件,我想从一个特定的文本文件一直读到文件的末尾

我可以用下面的方法来做

string path = @"C:\abc.txt";
var pp = File.ReadAllLines(path).Skip(1).Take(2);
但我只希望使用StreamReader完成此操作

我正在这样做,但没有给出正确的结果

using (Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
        using (StreamReader streamReader = new StreamReader(stream))
        {
                var str = streamReader.ReadLine().Skip(1).Take(2);
        }
}
我也可以用下面的方法来做。但是我想避免for循环

using (Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    using (StreamReader streamReader = new StreamReader(stream))
    {
                int count=2;
                for (int i = 0; i < count; i++)
                {
                    streamReader.ReadLine();
                }
                string data = streamReader.ReadLine();
    }
}
使用(Stream=File.Open(path,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
使用(StreamReader StreamReader=新StreamReader(stream))
{
整数计数=2;
for(int i=0;i
我的问题一点也不重复,前一个问题是关于读取所有文件,但我的问题是从特定行一直读到最后。

您只需要两行-
跳过(1)。取(2)
-为什么不直接读取它们

  using (StreamReader streamReader = new StreamReader(stream)) {
    streamReader.ReadLine(); // skip the first line

    // take next two lines
    string[] data = new string[] {
      streamReader.ReadLine(),
      streamReader.ReadLine(),
    }; 

    ...
  }
请注意,
StreamReader
的当前代码等于
Skip(2).Take(1).First()
。在一般情况下-
Skip(M).Take(N)
-必须用于循环(或其模拟):

使用(StreamReader StreamReader=新StreamReader(stream)){
//跳过第一个项目
对于(int i=0;i
您只需要两行-
跳过(1)。跳过(2)
-为什么不直接读呢

  using (StreamReader streamReader = new StreamReader(stream)) {
    streamReader.ReadLine(); // skip the first line

    // take next two lines
    string[] data = new string[] {
      streamReader.ReadLine(),
      streamReader.ReadLine(),
    }; 

    ...
  }
请注意,
StreamReader
的当前代码等于
Skip(2).Take(1).First()
。在一般情况下-
Skip(M).Take(N)
-必须用于循环(或其模拟):

使用(StreamReader StreamReader=新StreamReader(stream)){
//跳过第一个项目
对于(int i=0;i
skip(m)的可能重复。Take(n)…m和n可以是任何整数,因此我不能这样做,但我希望在没有循环的情况下这样做,因为它可以通过File.ReadAllLines()方法完成。所以,这种解决方案的缺点是,若我想在m之后读取,那个么读取前m行会浪费内存lines@viveknuna:
File.ReadAllLines()
通过使用循环(
while
)返回所有行(顺便说一句,
File.ReadLines()
在上下文中更有效),并且
Take(N)
也是基于循环的。请参阅以获取参考和。因此,您可以隐藏循环,但不能消除循环skip(m).Take(n)…m和n可以是任何整数,所以我不能这样做,但我希望在没有循环的情况下这样做,因为它可以通过File.ReadAllLines()方法完成。所以,这种解决方案的缺点是,若我想在m之后读取,那个么读取前m行会浪费内存lines@viveknuna:
File.ReadAllLines()
通过使用循环(
while
)返回所有行(顺便说一句,
File.ReadLines()
在上下文中更有效),并且
Take(N)
也是基于循环的。请参阅以获取参考和。因此,您可以隐藏循环,但不能消除它们