0

I found myself being stuck on a certain problem. Currently I am writing a program in C#, that reads in .txts. These .txts are constantly changing, as in content is being added to them.

I created a FileSystemWatcher, that hands the name of the file to the method reading in my txt. Now I would like to have something in between, that remembers the last position I read in the txt and then only passes on the new part.

It also needs to remember it the next time I start the program.

I thought about storing the last line in a xml or txt and then letting my method search that file for the last line noted there.

The only similar question I found was this: Read log file from last read position . I don't really understand it though.

Is there anyway how to do this more efficient or elegant?

Edit: I already linked the suggested question and no, it is not similar to mine. I am looking to permanently store the information on how far I am into a .txt, not just on runtime.

Community
  • 1
  • 1
  • Is your program closing in-between reads of the log file? If it isn't, then you don't need to store the value to a file. – BenVlodgi Jan 24 '17 at 16:39
  • A `Dictionary` seems like a reasonable option. You cold use the lower cased file path as the key and the last read position as the value. – InBetween Jan 24 '17 at 16:59
  • This sounds like two independent questions: 1) you are looking for a way to read only part of a file, given a known offset (stream.seek can do that) 2) you want to save an arbitrary value between restarts. (There are dozens of ways to do that. Ini-file, registry, app.config, perhaps even an NTFS alternative data stream) – HugoRune Jan 24 '17 at 17:25
  • @BenVlodgi Yes, it is! I don't really understand your answer though. –  Jan 24 '17 at 19:55
  • @InBetween I don't recall dictionary-objects to be stored permanently. –  Jan 24 '17 at 19:58
  • @HugoRune Exactly, but mainly I am looking to combine those two in a efficient and neat way. –  Jan 24 '17 at 19:59

2 Answers2

1

When you need to store a finite amount of information on program state on the Windows platform in between runtimes, the Windows Registry is usually the way to go. Here's a stackoverflow question that will show you how. Use HKEY_CURRENT_USER so that you don't have UAC/Admin Rights issues:

Writing to registry in a C# application

Community
  • 1
  • 1
hoodaticus
  • 3,772
  • 1
  • 18
  • 28
-1

.NET 4.0

Will this help? One way could be to store the line number instead of storing the content of the last line read.

Since .NET 4.0, it is possible to access a single line of a file directly. For instance, to access line 15:

string line = File.ReadLines(FileName).Skip(14).Take(1).First(); This will return only the line required

raring sunny
  • 191
  • 1
  • 3
  • 16
  • 1
    This code will still read the first 14 lines of the file behind the scenes, there is no performance benefit compared with reading and discarding the lines yourself. It is possible to access specific parts of a file only, but not with this syntax, and you need to know the byte offset, not just the line number. Stream.seek for example would work. – HugoRune Jan 24 '17 at 17:31
  • Got you! Will explore more on stream.seek. Thanks for an explanation. – raring sunny Jan 24 '17 at 18:00