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

时间:2015-12-01 04:52:54

标签: vb.net search explorer

使用VB.NET代码,如何打开资源管理器进程以根据表单中的条件搜索结果?例如:表单字段A中包含数字“1”,表单字段B中包含日期“2015-11-30”。通过在VB代码中连接这两个条件,我想启动Explorer进程并显示包含模式“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)

RUNTIME ERROR:

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()

我参考过的一系列网络资源链接:

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

2 个答案:

答案 0 :(得分:0)

试试这个,

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

希望这将成为您解决方案的一个星火。参考更多CommandLine Options

答案 1 :(得分:0)

经过多次试验&amp;错误我找到了一个解决方案,允许我的应用程序的搜索功能在Windows 7和10中都可以运行(它在7中崩溃)。

答案是我必须使用字符串Replace方法手动对Crumb Location进行URL编码。来自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)

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

感谢那些回答我指向正确方向的人。