【问题标题】:Reading Last Line of a text file C#读取文本文件的最后一行 C#
【发布时间】:2014-11-04 16:13:28
【问题描述】:

我正在使用以下代码来读取文本文件的最后一行:此代码有什么问题。我已经在下面编写了调试器生成的完整错误。我会犯什么错误?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
//using System.Linq;
namespace fileHandling
{
    class Program
    {
        public void GetDataFromFile()
        {


            // opening stream !!!
            FileStream fo = new FileStream("hello.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fo);

            if (!File.Exists("hello.txt"))
            {
                Console.WriteLine("{0} does not exist.", "hello.txt");

            }

            else
            {
                //string record;
                //record = sr.ReadLine();
                string lastLine = File.ReadLines("hello.txt").Last();
                Console.WriteLine(lastLine);
            }
            sr.Close();
           fo.Close();

        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.GetDataFromFile();
        }
    }
}

错误:

System.IO.IOException was unhandled
  HResult=-2147024864
  Message=The process cannot access the file 'C:\Users\nabeel\Documents\Visual Studio 2013\Projects\fileHandling\fileHandling\bin\Debug\hello.txt' because it is being used by another process.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
       at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
       at System.IO.StreamReader..ctor(String path, Encoding encoding)
       at System.IO.File.ReadLines(String path)
       at fileHandling.Program.GetDataFromFile() in c:\Users\nabeel\Documents\Visual Studio 2013\Projects\fileHandling\fileHandling\Program.cs:line 32
       at fileHandling.Program.Main(String[] args) in c:\Users\nabeel\Documents\Visual Studio 2013\Projects\fileHandling\fileHandling\Program.cs:line 60
  InnerException:

【问题讨论】:

  • 我的第一个猜测是该文件已在其他程序(文本编辑器等)中打开
  • 并尝试删除文件流和流阅读器,这也可能导致错误。

标签: c# file-handling


【解决方案1】:

您已打开已锁定文件的 FileStream,但随后不要使用它来读取文件,而是使用 File.ReadLines 只需删除以下几行即可。

FileStream fo = new FileStream("hello.txt", FileMode.Open);
StreamReader sr = new StreamReader(fo);

【讨论】:

  • 谢谢,我不确定问题是不是这么小,但是给我这样的初学者带来麻烦!
【解决方案2】:

你在这里打开文件:

FileStream fo = new FileStream("hello.txt", FileMode.Open);

那你就关不上了。

你甚至不需要那条线或它下面的那条线。只需删除它们。

(如果您保留这些行,请使用 fo.Close()fo.Dispose() 关闭文件。)

【讨论】:

    【解决方案3】:

    不要通过FileStream打开它,而是使用File.ReadLines

    删除这些行,看看是否更适合您

    FileStream fo = new FileStream("hello.txt", FileMode.Open);
    StreamReader sr = new StreamReader(fo);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-22
      • 2012-08-06
      • 2010-10-15
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      相关资源
      最近更新 更多