日期:2014-05-17  浏览次数:20998 次

C#怎样实时获取任务栏中文件夹并获取文件夹的路径
我用C#编写一个程序来获取任务栏中的文件夹(也就是说,这个程序能够知道电脑上哪些文件夹被打开),而且可以得这些文件夹的路径,当有新的文件夹打开时马上获取这个新打开的文件夹的路径,也就是能实时监控。比如说windows任务管理器的“应用程序”中就可以看到哪些文件夹被打开了。请问C#能实现这样的功能吗?

------解决方案--------------------
可以写BHO监视资源管理器
------解决方案--------------------
[code=C#][/code]
[DllImport("User32")]
private extern static int GetWindow(int hWnd, int wCmd);

[DllImport("User32")]
private extern static int GetWindowLongA(int hWnd, int wIndx);

[DllImport("user32", CharSet = CharSet.Auto)]
private extern static int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);

private const int GW_HWNDFIRST = 0;
private const int GW_HWNDNEXT = 2;
private const int GWL_STYLE = (-16);
private const int WS_VISIBLE = 268435456;
private const int WS_BORDER = 8388608;

public List<string> GetRunApplicationList(Form appForm)
{
try
{
List<string> appString = new List<string>();
int handle = (int)appForm.Handle;
int hwCurr;
hwCurr = GetWindow(handle, GW_HWNDFIRST);
while (hwCurr > 0)
{
int isTask = (WS_VISIBLE | WS_BORDER);
int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE);
bool taskWindow = ((lngStyle & isTask) == isTask);
if (taskWindow)
{
int length = GetWindowTextLength(new IntPtr(hwCurr));
StringBuilder sb = new StringBuilder(2 * length + 1);
GetWindowText(hwCurr, sb, sb.Capacity);
string strTitle = sb.ToString();
if (!string.IsNullOrEmpty(strTitle))
{
appString.Add(strTitle);
}
}
hwCurr = GetWindow(hwCurr, GW_HWNDNEXT);
}
return appString;
}
catch (Exception ex)
{
throw new ApplicationInfoException("读取应用程序信息时出错:" + ex.Message);
}
}


------解决方案--------------------
比较懒的话通过IShellWindows获得资源管理器窗口列表,之后通过IWebBrowser2访问URL
这个方法在Vista下被UAC挡了,要准确获得的话还是得写BHO