0

I have looked at many posts with the similar title and their suggested answers. But they haven't solved my problem. Examples are this, this and this.

I have a text file that is always locked by a Windows Service (it's a logger basically). What I want to do is to read from this file in my program and also be able to empty its contents.

Do we have a working solution that does not include copying the file?

This is my code for reading the file so far and it of course is not currently working.

serviceLogs = new List<string>();
try
{
    //Lock Exclusively the file
    using (FileStream fileStream = new FileStream(Globals.serviceLogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        using (StreamReader sr = new StreamReader(fileStream))
        {
            String line;
            while ((line = sr.ReadLine()) != null)
                serviceLogs.Add(line);
        }
    }
Community
  • 1
  • 1
disasterkid
  • 6,948
  • 25
  • 94
  • 179
  • Can you modify the service? – CodesInChaos Dec 17 '14 at 15:46
  • Why are you opening the same file twice? Once with `File.Open` (without using the result) and once via the `FileStream` constructor? – CodesInChaos Dec 17 '14 at 15:47
  • @CodesInChaos yes I have written the better part of the service myself. I can stop the service, read from the file and edit it and then restart it, but this is a very bad idea. Since my service has more important things to do to stop itself so the user reads its log file. – disasterkid Dec 17 '14 at 15:48
  • @CodesInChaos oh yes. Sorry I will delete that line. – disasterkid Dec 17 '14 at 15:49
  • My point is that you need appropriate sharing flags on both the service and the program. You need to modify the service so that it opens the file allowing the program read and write access. – CodesInChaos Dec 17 '14 at 15:49
  • `Not Currently working` is not a good thing in regards to either helping you or solving the issue at hand.. have you tried debugging the code and seeing what type of errors you are getting..? can you provide more information – MethodMan Dec 17 '14 at 15:52

2 Answers2

1

A few different approaches:

  1. Don't share a plain file between the service and the program.

    Perhaps a pipe or socket is better for your application.

  2. Reduce the time the service keeps the file open. For example by using File.AppendAllLines or File.AppendAllText.

    This can still lead to lock conflicts, but you should be able to handle them by retrying.

  3. Don't clear the file from the program. Have it send a message to the service which then clears the file.

    Taking this approach one step further, you wouldn't even read the file from the program, but query the service.

  4. Change both the service and the program so they open the file with permissive sharing flags.

    Both service and program should use FileShare.ReadWrite. This has the risk of conflicting operations corrupting the data.

Community
  • 1
  • 1
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
1

If you can change the service, I recommend adding some form of inter-process communication whereby the client program can request a copy of the data and, either in the same request or another, ask the server to clear the file. I've never used WCF, but from what little I know it sounds like it was designed for this type of thing. If you want a third-party solution for IPC, I recommend Ice from ZeroC.

adv12
  • 8,443
  • 2
  • 24
  • 48