开发者

File is being used by another process during a while loop (C#)

I currently have a while loop, which contains an if statemen开发者_StackOverflowt:

if (s.Contains("mp4:production/CATCHUP/"))

Although when this condition is true, and I try to use other methods (as seen below e.g. RemoveEXELog) I get an access denied a process is currently using the file "Command.bat".

How could I stop looping the file when I execute my other methods?

private void CheckLog()
{
    while (true)
    {
        Thread.Sleep(5000);
        if (!System.IO.File.Exists("Command.bat")) continue;
        using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat"))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {
                if (s.Contains("mp4:production/CATCHUP/"))
                {

                    RemoveEXELog(); // Deletes a specific keyword from Command.bat

                    Process p = new Process();
                    p.StartInfo.WorkingDirectory = "dump";
                    p.StartInfo.FileName = "test.exe";
                    p.StartInfo.Arguments = s;
                    p.Start();

                    ClearLog(); // Deletes Command.bat and then creates a new empty Command.bat
                }
            }
        }
    }
}


The other solutions should work for you, however, you could also open the file like so:

using (var sr = new FileStream("Command.bat", FileMode.Open, 
                               FileAccess.Read, FileShare.ReadWrite))
{
    ...
}

This opens the file in read-only mode.

Then in your RemoveEXELog() method you could open it like so:

using (var sr = new FileStream("Command.bat", FileMode.Open, 
                               FileAccess.ReadWrite, FileShare.ReadWrite))
{
    ...
}

This approach should allow you to open the file from both locations without getting an I/O exception that the file is already in use.


private void CheckLog()
    {
        bool _found;
        while (true)
        {
            string s = "";
            _found = false;
            Thread.Sleep(5000);
            if (!System.IO.File.Exists("Command.bat")) continue;
            using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat"))
            {
                while ((s = sr.ReadLine()) != null)
                {
                    if (s.Contains("mp4:production/CATCHUP/"))
                    {
                       _found = true;
                       break;
                    }

                }
            }
            if (_found)
            {
                 RemoveEXELog(); // Deletes a specific keyword from Command.bat

                 Process p = new Process();
                 p.StartInfo.WorkingDirectory = "dump";
                 p.StartInfo.FileName = "test.exe";
                 p.StartInfo.Arguments = s;
                 p.Start();

                 ClearLog(); // Deletes Command.bat and then creates a new empty Command.bat
            }
        }
    }


Am I right tha this lines of your code:

RemoveEXELog(); // Deletes a specific keyword from Command.bat
ClearLog(); /

are doing something with Command.bat file? If so, you must move them out of

using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat"))

block, because this reader prevents other programs edit the file.

Try to use some bool flags:

bool needRemove = false, needClear = false;
using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat"))
{
    string s = "";
    while ((s = sr.ReadLine()) != null)
    {
        if (s.Contains("mp4:production/CATCHUP/"))
        {
            needRemove = true;

            Process p = new Process();
            p.StartInfo.WorkingDirectory = "dump";
            p.StartInfo.FileName = "test.exe";
            p.StartInfo.Arguments = s;
            p.Start();

            needClear = true;
        }
    }
}

if (needRemove) RemoveEXELog(); // Deletes a specific keyword from Command.bat
if (needClear) ClearLog(); // Deletes Command.bat and then creates a new empty Command.bat
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜