简体   繁体   中英

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

Using VB.NET code, how can I open the Explorer process to search results based on criteria from a form? For example: form field A has the number "1" in it, and form field B has the date "2015-11-30" in it. By concatenating these two criteria in the VB code, I want to start the Explorer process and display all the file names within a given root folder that contains the pattern "1 2015-11-30". (Essentially, it would be the same results I would get typing the above into the Explorer Search box in Windows 7, except I need to code that.)

UPDATE Jan 25: I have come back to this last remaining issue after completing all other project coding and I am still having a problem in Windows 7 (works fine in Windows 10) after much research. Some additional helpful info I can offer this time is shown below. Note, I have also tried the AQS Search syntax using url encoding on the Crumb location and still get the same runtime error. Also note, I do NOT get a runtime error when I leave out the Crumb argument -- but that is of little practical use to me, just an observation.

Finally, I have also tried the code on other Win 7 machines at the corporate offices and get the same runtime error. Perhaps someone can try the code below snippet below on their Win 7 and advise what they get?

IMPORTED ASSEMBLIES:

    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

SEARCH BUTTON CODE SNIPPET, INCLUDING MESSAGEBOX OUTPUT OF THE SEARCH STRING:

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

A COUPLE OF WEB RESOURCE LINKS I REFERENCED OFTEN:

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

try with this,

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

Hope this Will Be a Spark to your solution.Reference on more CommandLine Options

After much trial & error I found a solution that allows my app's Search function to work in both Windows 7 and 10 (it crashed in 7).

The answer was that I had to manually URL-encode the Crumb Location using the string Replace method. The URLEncode method from HttpUtility did not encode the whole location string for reasons I still don't know.

My solution below:

    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)

There may be a better way but this works for me.

Thank you to those who replied for pointing me in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM