使用文件名搜索模式打开资源管理器进程 (VB.NET)

Open Explorer process with a search pattern on file name (VB.NET)

使用 VB.NET 代码,如何打开 Explorer 进程以根据表单中的条件搜索结果?例如:表单域 A 中有数字“1”,表单域 B 中有日期“2015-11-30”。通过在 VB 代码中连接这两个条件,我想启动资源管理器进程并显示包含模式“1 2015-11-30”的给定根文件夹中的所有文件名。 (基本上,这与我在 Windows 7 中的“资源管理器搜索”框中输入上述内容的结果相同,只是我需要对其进行编码。)

1 月 25 日更新:在完成所有其他项目编码后,我又回到了最后一个遗留问题,我在 Windows 7 中仍然遇到问题(在 Windows 10 中工作正常)很多研究。这次我可以提供的一些额外的有用信息如下所示。请注意,我还尝试了在 Crumb 位置使用 url 编码的 AQS 搜索语法,但仍然出现相同的运行时错误。另请注意,当我省略 Crumb 参数时,我不会收到运行时错误——但这对我来说没有什么实际用处,只是一个观察。

最后,我也在公司办公室的其他 Win 7 机器上尝试了代码,并得到了同样的运行时错误。也许有人可以在他们的 Win 7 上尝试下面代码片段下面的代码并告知他们得到了什么?

进口组件:

    Imports System
    Imports System.IO
    Imports System.Text
    Imports System.Drawing.Imaging
    Imports System.Threading
    Imports System.Configuration
    Imports System.Net.Mail
    Imports Microsoft.Win32
    Imports System.Web
    Imports System.ComponentModel
    Imports System.Windows

搜索按钮代码片段,包括搜索字符串的消息框输出:

    Dim searchString As String
    Dim Search As String
    searchString = ControlChars.Quote & "     " & Me.DateTimePicker1.Value.Year.ToString & "-" & Me.DateTimePicker1.Value.Month.ToString().PadLeft(2, "0") & "-" & Me.DateTimePicker1.Value.Day.ToString().PadLeft(2, "0") & ControlChars.Quote
    Search = "search-ms:query=System.FileName:~=" & searchString.ToString & "&crumb=location:" & strArchiveDirectory_SHARED & ",recursive" & "&" 
    MessageBox.Show(Search) 'Messagebox shows: search-ms:query=System.FileName:~="     2016-01-25"&crumb=location:C:\EPS\Enforcement Photos Archive_DEMO,recursive&
    Process.Start(Search)

运行时错误:

System.ComponentModel.Win32Exception was unhandled
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=The system cannot find the file specified
  NativeErrorCode=2
  Source=System
  StackTrace:
       at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)
   at EPS.Form1.btnSearch_Click(Object sender, EventArgs e) in E:\Code Development\Visual Studio 2013\EPS\EPS\Form1.vb:line 622
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
   at System.Windows.Forms.Control.WmCommand(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   at EPS.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

我经常引用的几个 Web 资源链接:

https://msdn.microsoft.com/en-us/library/ff684385.aspx https://msdn.microsoft.com/en-us/library/windows/desktop/cc144080(v=vs.85).aspx

试试这个,

dim s as string= TextBox1.text & Textbox2.text
dim srch as string= "search-ms:query="& s &"&crumb=location:<Yourlocation>&"
Process.start(srch)

希望这会成为您 solution.Reference 在更多 CommandLine Options

上的火花

经过多次尝试和错误后,我找到了一个解决方案,允许我的应用程序的搜索功能在 Windows 7 和 10 中工作(它在 7 中崩溃)。

答案是我必须使用字符串 Replace 方法手动 URL 对 Crumb Location 进行编码。 HttpUtility 的 URLEncode 方法没有对整个位置字符串进行编码,原因我仍然不知道。

我的解决方案如下:

    Dim urlEncodedLocationA As String = strArchiveDirectory_SHARED.Replace(":", "%3A")
    Dim urlEncodedLocationB As String = urlEncodedLocationA.Replace("\", "%5C")
    Dim urlEncodedLocationC As String = urlEncodedLocationB.Replace(" ", "%20")

    searchString = ControlChars.Quote & Me.txtBadgeNumber.Text & "     " & Me.DateTimePicker1.Value.Year.ToString & "-" & Me.DateTimePicker1.Value.Month.ToString().PadLeft(2, "0") & "-" & Me.DateTimePicker1.Value.Day.ToString().PadLeft(2, "0") & ControlChars.Quote
    Search = "search-ms:query=System.FileName:~<" & searchString.ToString & "&crumb=Location:" & urlEncodedLocationC & ",recursive" & "&" 'Search STARTS (~<) with the badge/date string.
    Process.Start(Search)

可能有更好的方法,但这对我有用。

感谢那些给我指出正确方向的回复。