0

Background:

I'm currently working on a project that reads log files from a game called Hearthstone. The idea is that the program monitors any changes made to the log file (which I'm doing by checking it's size change) and then triggers an event that reads the log file for certain keywords.

Problem:

The problem I am currently facing is that when the event triggers I get the 'File is in use by another process'-error. As a work around I've made an FileStream which gives read and write access to the log file. This does create the necessity of having the program on first before starting up Hearthstone, but it's a necessity I'm willing to work with.

Now my understanding is that this should make the log file open to any other process that needs to read and/or write (to) the file. However when I startup Hearthstone, it now doesn't write to the log file anymore.

Code: These are all in the same class.

The FileStream:

static string filePath = "C:\\Program Files (x86)\\Hearthstone\\Logs\\Achievements.log";
    FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

The Watch method:

private void Watch()
    {
        string fileName = "Achievements.log";

        watcher.Path = filePath;
        watcher.Filter = fileName;
        watcher.NotifyFilter = NotifyFilters.Size;
        watcher.EnableRaisingEvents = true;

        watcher.Changed += new FileSystemEventHandler(OnChange);
    }

The OnChange event:

private void OnChange(object sender, FileSystemEventArgs e)
    {
        // Debugger:
        Console.WriteLine("Debugger: OnChange event fired!");

        if (watcher.EnableRaisingEvents == true && SecondCall == 1)
        {
            watcher.EnableRaisingEvents = false;

            ReadLines(e.FullPath);

            OnSaveCommand();
            ResetState();
        }
        else
        {
            SecondCall += 1;
        }
    }

The ReadLines method:

private void ReadLines(string filePath)
    {
        using (fileStream)
        {
            var lastLine = File.ReadLines(filePath).Last();
            // Debugger:
            Console.WriteLine("Debugger: " + lastLine);
        }
    }

Question:

Is it possible to access the log file, while having it in use by the Hearthstone program?

  • 2
    Possible duplicate of [What's the least invasive way to read a locked file in C# (perhaps in unsafe mode)?](https://stackoverflow.com/questions/3560651/whats-the-least-invasive-way-to-read-a-locked-file-in-c-sharp-perhaps-in-unsaf) – ProgrammingLlama Jun 20 '18 at 07:54
  • 1
    You should put the flag FileAccess.Read instead of FileAccess.ReadWrite when you open the file – J.Loscos Jun 20 '18 at 07:54
  • If the File is read locked, and wants an exclusive lock, there the only thing you can do is use shadowcopy, or write a dll injection lib to proxy the call that opens the file and give it less restrictive share option. i belive there is some shadow copy .net libs floating around the place. also, dll injection using madshi madhook libs is fairly easy as well – TheGeneral Jun 20 '18 at 07:57

0 Answers0