1
private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
    errorLogFileProcess = Process.Start(AppVars.ErrorLogFilePath);
}

The above opens a text file in notepad. It contains error logs for my application.

I do not want to allow the user to open multiples of this file. So I do not want to have him/her click the button again and open another window of the same file.

How do I check if this file is open? Note: I DO NOT want to close a (or all) notepad.exe processes because the user might have a notepad process open for something else other than my application file (which uses notepad.exe when the file is open).

So again, how do I go about checking whether the process I opened is already opened?

JJ.
  • 9,580
  • 37
  • 116
  • 189
  • 2
    What actual problem are you trying to solve? Is the issue that the user has an older copy of the logs open as well and gets confused as to which is the latest? If so, Notepad replacements such as Notepad++ and SciTE will pickup modifications and reload the content. – devstuff Sep 05 '12 at 23:47
  • Why don't you just use a window your application owns, and put a multi-line TextBox in there. Add a save and a copy-to-clipboard button, and you're done. – CodesInChaos Sep 06 '12 at 00:03
  • "The above opens a text file in notepad." It opens a file with the program associated with that file type. That isn't necessarily notepad. – CodesInChaos Sep 06 '12 at 00:04

2 Answers2

2

Just had another re-read of your question.

If you want to force the user to close the active instance of Notepad (that you launched) before allowing them to open it again, keep the errorLogFileProcess instance around and then recheck it before allowing the command.

Process errorLogFileProcess;

private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
    if (errorLogFileProcess != null)
    {
        // Process was launched previously; check if exited.
        if (!errorLogFileProcess.HasExited)
        {
            throw new Exception("Can't launch until first one closed.");
        }
    }

    errorLogFileProcess = Process.Start(AppVars.ErrorLogFilePath);
}

Could also use the errorLogFileProcess.HasExited state to disable the button so that the Click event is not raised.

devstuff
  • 8,277
  • 1
  • 27
  • 33
  • You should start notepad explicitly. Your code will fail for some other programs that might be associated with the file type in question. – CodesInChaos Sep 06 '12 at 00:06
  • Just duplicated the OP's code; personally I always determine the full path for whatever I'm launching. – devstuff Sep 06 '12 at 00:07
  • instead of throwing the exception, how would I move the notepad window to front so that the user can see it? – JJ. Sep 06 '12 at 00:24
  • The process will start the first time through (since `errorLogFileProcess` will be null), but it will stop until Notepad is closed. – devstuff Sep 06 '12 at 05:02
  • BTW, you can't easily control the windows of another process with recent releases of Windows. Microsoft want the user to be in control of this sort of thing. – devstuff Sep 06 '12 at 05:04
  • @devstuff, I have your exact code and when I click on the button twice, it opens notepad twice... – JJ. Sep 06 '12 at 15:19
  • Perhaps you're using a Notepad clone that uses a launch process which dies after launching the real app (a number of self-updating apps do this). Using shell associations may also end up using multiple processes. Change the last line to have 2 arguments, the first being the full path to Notepad.exe, the second as above. Works for me. – devstuff Sep 11 '12 at 05:26
-1

Check here how to check if file is already opened (by using try, catch blocks): Check if a file is open

Community
  • 1
  • 1
Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30