Question

From my application, I allow the user to open the help file.

The help file is only only a *.chm file which is opened with the default help.

I open it with the following command

Process.Start(stringPathToTheFile);

The issue is that the user can click twice or more on the help menu and the file will open as many time as they click.

My first problem is that I must ensure that the user cannot open the help file more than once:

I'm aware of the process.HasExcited property, but I can't use this because if I open my software, click on the help, close my software, open it again and click on the help I end up with two help files open.

EDIT It seems that this is not very clear, so here is a small sample of what I mean.

Create a console application with this:

private static void Main()
{
    String file = @"c:\testFile.txt";
    while (true)
    {
        Console.ReadLine();
        OpenProcessIfNeeded(file);
    }
}

private static void OpenProcessIfNeeded(String file)
{
    //Do the check here
    if (true)
    {
        Process process = Process.Start(file);
    }
}

if I use HasExited(or the event), I will have this kind of code:

private static readonly Dictionary<String, Process> _startedProcess = new Dictionary<string, Process>();

private static void Main()
{
    String file = @"c:\testFile.txt";
    while (true)
    {
        Console.ReadLine();
        OpenProcessIfNeeded(file);
    }
}

private static void OpenProcessIfNeeded(String file)
{
    if (!_startedProcess.ContainsKey(file) || _startedProcess[file].HasExited)
    {
        Process process = Process.Start(file);
        _startedProcess[file] = process;
    }
}

This work when you keep the console application open, but if you close the application and re-open it, it won't work since _startedProcess will not contains the process.

And I can't find the correct process with Process.GetProcesses() because I don't see any property that allows me to know which process it is.

So, how can I see if a process is currently displaying my file? I can't search the process, since the process(hh.exe) can be used to read other file.

My second wish is to focus on the existing process if I've already opened this file.

Thank you for your help

Was it helpful?

Solution

I found a way to check if one process has the file in his "CommandLine", meaning that it is currently editing the application:

private static void OpenProcessIfNeeded(String file)
{
    using (ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process"))
    {
        foreach (ManagementObject mo in mos.Get())
        {
            if (mo["CommandLine"] != null &&
                mo["CommandLine"].ToString().IndexOf(file, StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                Console.WriteLine("Found!: " + mo["CommandLine"]);
                return;
            }
        }
    }
    Process.Start(file);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top