Reading Last Line of a text file C# -


I am using the following code to read the last line of the text file: What is wrong with this code? I have written the complete error generated by the debugger below, what would be the mistake I am doing?

  using the system; Using System.Collections.Generic; Using System.Linq; Using System.Text; System usage threading. Task; Using System.IO; // Using System.Linq; Namespace file {class program {public void GetDataFromFile () {// Opening stream !!! FileStream fo = new filestream ("Hello.txt", FileMode.Open); Streamer sr = new streamer (for); If (! File.example ("hello.txt")) {Console.WriteLine ("{0} does not exist.", "Hello.txt"); } Other {// string record; // record = sr.ReadLine (); String lastline = file. Readline ("helotext"). Last (); Console.WriteLine (lastLine); } Sr.Close (); Fo.Close (); } Static zero main (string [] args) {program p = new program (); P.GetDataFromFile (); }}}  

Error:

  System.IO.IOException was without action HRESULT = - + 2147024864 Message = Process file 'C can not be used: \ Users \ Nabeel \ Documents \ Visual Studio 2013 \ Projects \ fileHandling \ fileHandling \ bin \ Debug \ hello.txt 'because it is being used by another process source = mscorlib StackTrace: System.IO.FileStream.Init on System IO .__ Error.WinIOError (Int32 errorCode, string maybeFullPath) (on string path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, fileshare share, Int32 bufferSize, FileOptions Option, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) on System.IO.FileStream..ctor (string path, FileMode mode, FileAccess usage, fileshare share, Int32 bufferSize, FileOptions option, String msgPath, Boolean BFromProxy, Boolean useLongPath, Boolean checkHost) on System.IO.StreamReader..ctor (string path, encoding encoding, boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost) on System.IO.StreamReader..ctor (string path, Encoding encoding) System.IO.File.ReadLines (string path) in fileHandling.Program.GetDataFromFile () on: \ Users \ n File \ Handels \ Documents \ \ Projects \ Visual Studio 2013 \ fileHandling \ fileHandli ng \ Program.cs: FileHandling at line c: fileHandling.Program.Main (String [] args) 32: \ Users \ Nabil \ Documents \ Visual Studio 2013 \ projects \ fileHandling \ FileHandling \ Program.cs: Line 60 InnerException:  

You have opened the filestream which File is locked, but after you do not use it instead of reading the file you are using the file. Remove redline lines only and it all works fine.

  FileStream fo = new FileStream ("hello.txt", FileMode.Open); Streamer sr = new streamer (for);  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -