0

I am coding an app to read a game's chat log. The problem is that I get an error randomly, maybe the app works for 20 min or 30 min and suddenly get the error: System.IO.IOException: "The process cannot access the file 'fileDir' because it is being used by another process."

I know that the chat log is being used by the game but as I said I can read the lines or even delete the whole text after reaching 1,000 lines without problem until I get that error. I also can edit the file manually and save it while ingame.

private void start_Click(object sender, EventArgs e){
      if (!_isRunning)
      {        
          _isRunning = true;                     
          StreamReader sr = new StreamReader(fileDir);
          var lines = File.ReadAllLines(fileDir);
          lastReadLine = lines.Length - 1; //start to read from the last line 
          sr.Close();                   
          timer1.Start();
      }
}  
private void UpdateText()
{
    try
    {
        StreamReader sr = new StreamReader(fileDir); // error here
        var lines = File.ReadAllLines(fileDir);
        sr.Close();
        linesLength = lines.Length;
        for (int i = lastReadLine; i < lines.Length; i++)
        {
            if (lines[i].Contains("You inflicted") || lines[i].Contains("The attack missed") || lines[i].Contains("The target Dodged"))
            {
                totalAttacks++; 
            }
            lastReadLine = i + 1;
        }

        if (linesLength >= linesMax)
        {
            try
            {
                if (File.Exists(fileDir))
                {
                    TextWriter tw = new StreamWriter(fileDir, false); //error here 1 time
                    lastReadLine = 0;
                    tw.Close();
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
    catch (IOException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

The timer executes the UpdateText() 1 times per second. It's my first app with Visual Studio and I am also newbie with C#, I hope any experienced programmer knows what's wrong.

Thank you.

Arkshija
  • 77
  • 6
  • 3
    Why are you even creating `StreamReader`s? You're never using them... – Broots Waymb May 22 '19 at 14:04
  • 1
    Ok I feel stupid. – Arkshija May 22 '19 at 14:10
  • Your issue is almost definitely what @Çöđěxěŕ listed above - the file being locked while your trying to access. Here is another question that both solves that issue and the file polling you're attempting. [Reading file content changes in net](https://stackoverflow.com/questions/2373090/reading-file-content-changes-in-net) – Riggeot May 22 '19 at 14:33
  • I read everything but I dont get it :s – Arkshija May 22 '19 at 16:08

1 Answers1

0

Quick note: the StreamReader isn't even being used.

I've come up with a basic answer, to replace the code which reads the lines of a file:

List<string> lines = null;
using (var sr = new StreamReader("file.txt"))
{
    var text = sr.ReadToEndAsync().Result;
    lines = text.Split('\n').ToList();
}
Reece Russell
  • 347
  • 1
  • 11
  • So how should i write the code to delete the text file content if lines are higher than 1000 without getting that error? should i add the stramwritter inside the using or make a new one? – Arkshija May 22 '19 at 17:04
  • @Arkshija so if the file has more than 1000 lines you would like to delete all of the lines or just the lines over 1000? – Reece Russell May 23 '19 at 09:00