c# 判断任务栏中是否已经打开文件夹?

2025-04-15 09:27:03
推荐回答(2个)
回答(1):

    //using System.Diagnostics;
    //using System.Runtime.InteropServices;
    //using SHDocVw;
    class Program
    {
        //1. 引用c:\windows\system32\shdocvw.dll
        //2. 声明SetForegroundWindow
        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        static void Main(string[] args)
        {
            ShellWindows wins = new ShellWindows();
            string folder = @"D:\test";
            bool found = false;
            foreach (InternetExplorer w in wins)
                if (w.LocationURL.Contains(
                    folder.Replace('\\', '/')))
                {
                    SetForegroundWindow((IntPtr)w.HWND);
                    found = true;
                    break;
                }
            if (!found)
                Process.Start("explorer", folder);
            Console.ReadLine();
        }
    }

回答(2):

网页链接 net3.5下使用,要使用win32api来达到目的

///

/// 置前窗口

///

///

///

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern bool SetForegroundWindow(IntPtr hwnd);

Win32api.SetForegroundWindow(item.HWND);