开发者

How do check if another application window is open on my machine (i.e., iterate through all open windows)?

I have an app that writes a series of text files to a folder on the user's machine. It then prompts them if they would like to open the folder to view all the files. I use System.Diagnostics.Process.Start() to do this and it wor开发者_如何学Cks well. However, if there is already a window with that folder open, I would just like to reuse that window instead of opening another one. Helps keep things tidy.

Any ideas?

Update: I tried this:

        if (MessageBox.Show("Compiled!  Open folder?", "Compile Done", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
        {
            ProcessStartInfo Info = new ProcessStartInfo("explorer", Path);
            Info.UseShellExecute = false;

            Process.Start(Info);
        }

But it still opens a new window. I also tried setting UseShellExecute to true and I get the same result of a new window opening up.


Just tried this on Windows XP:

System.Diagnostics.Process.Start(@"C:\Dev");
System.Threading.Thread.Sleep(10000);
System.Diagnostics.Process.Start(@"C:\Dev");

And it opened my C:\Dev folder. I then focused elsewhere. After 10 seconds, the C:\Dev window flashed and was highlighted again. So what are you doing different where it isn't working?

(Trying to find out if there's an open window displaying a particular folder is fraught and error prone. I wouldn't recommend it)


If you use ShellExecute() to open the folder then Explorer will do the work for you and re-use an open Explorer window if one exists.

The .net way to call ShellExecute() is to set the UseShellExecute property of the ProcessStartInfo object that is passed to Process.Start().


Update

Having now posted your code I can see the problem. You should ask the system to open the folder rather than trying to start a new explorer.exe process, as so:

ProcessStartInfo Info = new ProcessStartInfo(Path);
Process.Start(Info);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜