2014-11-04 453 views
-2

我正在使用以下一段代碼來讀取文本文件的最後一行:此代碼有什麼問題。我已經寫下了調試器生成的完整錯誤。我正在做什麼錯誤?讀取文本文件的最後一行C#

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: 
+0

我的第一個猜測更好的將是文件被保存在其他程序中打開(文本編輯器等) – nozzleman 2014-11-04 16:15:50

+0

並嘗試刪除該文件流和數據流讀寫器,這也可能導致錯誤。 – nozzleman 2014-11-04 16:17:32

回答

3

你已經打開已鎖定文件的的FileStream,但那麼就不要使用它來閱讀文件,而不是你正在使用File.ReadLines只是刪除下面的行,它都工作正常。

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

謝謝,我不確定問題是如此之小,但給像我這樣的初學者帶來麻煩! – EM923 2014-11-04 16:23:00

1

你打開此文件:

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

然後你無法將其關閉。

你甚至不需要那條線或它下面的那條線。只要刪除它們。

(如果你在保留這些行,用fo.Close()fo.Dispose()關閉文件。)

1

不要通過FileStream打開它,並使用File.ReadLines

刪除這些行,看看是否適合你

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