c# 判断任务栏中是否已经打开文件夹?
答案:1 悬赏:0 手机版
解决时间 2021-11-12 14:50
- 提问者网友:情歌越听越心酸
- 2021-11-11 15:18
c# 判断任务栏中是否已经打开文件夹?
最佳答案
- 五星知识达人网友:人间朝暮
- 2021-11-11 15:50
//using System.Diagnostics;
//using System.Runtime.InteropServices;
//using SHDocVw;
class Program
{
//1. 引用c:windowssystem32shdocvw.dll
//2. 声明SetForegroundWindow
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
static void Main(string[] args)
{
ShellWindows wins = new ShellWindows();
string folder = @"D: est";
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();
}
}追问ShellWindows 这个需要依赖哪个dll呢?追答看上面注释。追问你的这个在WIN7上不好使,我是把它放在一个按钮的点击事件里面,点击第二次,同样的打开一个新的窗口,并没有前置就的窗口。而且最后一行代码读取一行就报错了,不知道为什么要写这么一行,能解释一下吗。谢谢。追答readline不是必要的,自己可以去掉。你打开的路径是什么样的?我在自己的win7上测过了的。
//using System.Runtime.InteropServices;
//using SHDocVw;
class Program
{
//1. 引用c:windowssystem32shdocvw.dll
//2. 声明SetForegroundWindow
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
static void Main(string[] args)
{
ShellWindows wins = new ShellWindows();
string folder = @"D: est";
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();
}
}追问ShellWindows 这个需要依赖哪个dll呢?追答看上面注释。追问你的这个在WIN7上不好使,我是把它放在一个按钮的点击事件里面,点击第二次,同样的打开一个新的窗口,并没有前置就的窗口。而且最后一行代码读取一行就报错了,不知道为什么要写这么一行,能解释一下吗。谢谢。追答readline不是必要的,自己可以去掉。你打开的路径是什么样的?我在自己的win7上测过了的。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯