是否有一些C#代码用于从资源管理器搜索框中输入的搜索词创建保存的查询文件(.search-ms),其方式与ISearchQueryHelper将搜索词扩展为Windows搜索API的OLEDB查询的方式相同?或者将OLEDB查询转换为.search-ms文件的代码?我想在资源管理器中显示Windows搜索的结果 - 我可以在网格中显示OleDb查询结果,但更喜欢使用资源管理器。
答案 0 :(得分:0)
原来,创建已保存的搜索然后运行它并不是启动显示已过滤结果的Windows /文件浏览器窗口的最简单方法,因为保存的搜索XML非常复杂,无法以编程方式生成。将搜索框左侧的框内容作为URL并提供给 Internet Explorer,可以得到正确的结果。
输入搜索字符串后
name:~ "*[*2602_Australia_Australian Capital Territory_Downer*].*" OR name:~ "*[*2602_Australia_Australian Capital Territory_O'Connor*].*"
在文件夹
中C:\Users\Simon\Pictures\iPhoneSample
显示预期结果。单击显示“在iPhoneSample中搜索结果”的左侧框显示:
search-ms:displayname=Search%20Results%20in%20iPhoneSample&crumb=name%3A~"*[*2602_Australia_Australian%20Capital%20Territory_Downer*].*"%20OR%20name%3A~"*[*2602_Australia_Australian%20Capital%20Territory_O'Connor*].*"&crumb=location:C%3A%5CUsers%5CSimon%5CPictures%5CiPhoneSample
将此作为URL提供给 Internet 资源管理器(iexplore.exe而非Explorer.exe)显示带有预期搜索结果的新Windows /文件资源管理器窗口。执行此操作的一些C#代码如下所示:
/// <summary>
/// Show Windows Explorer in new window with results filtered by search string
/// </summary>
/// <param name="sCommonFolder">Folder to search in </param>
/// <param name="sSearchString">search string</param>
private void ShowSearchResultsInExplorer(string sCommonFolder, string sSearchString)
{
// sCmd = "search-ms:displayname=Search%20Results%20in%20iPhoneSample&crumb=name" & _
//"%3A~""*[*2602_Australia_Australian%20Capital%20Territory_Downer*].*""%20OR%20name%3A~""" & _
//"*[*2602_Australia_Australian%20Capital%20Territory_O'Connor*].*""&crumb=location:C%3A%5CUsers%5CSimon%5CPictures%5CiPhoneSample"
string sFilter = "search-ms:displayname=";
sFilter += Uri.EscapeDataString("Tagged files in " + sCommonFolder);
sFilter += @"&crumb=" +sSearchString;
sFilter += @"&crumb=location:" + Uri.EscapeDataString(sCommonFolder);
Process.Start("IExplore.exe", sFilter);
}
显示搜索结果后,需要关闭窗口才能显示后续搜索结果,否则不会显示后续结果。这可以使用以下代码完成:
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer window in shellWindows)
{
Console.Write ("Window " + window.LocationName + " " + window.LocationURL + " " + window.Name + " " + window.HWND + "\r\n");
if (window.LocationName.Contains("Tagged files"))
{
SendMessage((IntPtr)window.HWND, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
此代码需要一些声明:
using System.Web;
using System.Runtime.InteropServices;
....
static uint WM_CLOSE = 0x10;
...
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);