Read/Delete top line from a File

  • Thread starter J Wolfgang Goerlich
  • Start date
J

J Wolfgang Goerlich

Hello group,

I am looking for an easy way to read the top line from a text file and
delete it. The text file is a working queue. If no lines exist, the
thread simply waits.

I am running into difficulty because it seems that I need a
StreamWriter and StreamReader with a lock on the same file.

Does anyone have some precanned code samples for popping the top line
off a file?

Thanks in advance,

J Wolfgang Goerlich
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello group,

I am looking for an easy way to read the top line from a text file and
delete it. The text file is a working queue. If no lines exist, the
thread simply waits.

I am running into difficulty because it seems that I need a
StreamWriter and StreamReader with a lock on the same file.

Does anyone have some precanned code samples for popping the top line
off a file?

Thanks in advance,

J Wolfgang Goerlich

It cannot be simpler than this:
-open file to read
-create tmp file using Path.GetTempfileName
remove the first line of the file
dump the rest of the file to the temp file
close both files
copy from tmp to real file
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top