[英]Open Explorer process with a search pattern on file name (VB.NET)
使用VB.NET代碼,如何打開資源管理器進程以根據表單中的條件搜索結果? 例如:表單字段A中具有數字“ 1”,表單字段B中具有日期“ 2015-11-30”。 通過在VB代碼中串聯這兩個條件,我想啟動資源管理器進程並顯示包含模式“ 1 2015-11-30”的給定根文件夾內的所有文件名。 (從本質上講,這與我在Windows 7中的“資源管理器搜索”框中鍵入上述內容的結果相同,只是我需要對此進行編碼。)
1月25日更新:完成所有其他項目編碼后,我又回到了最后一個問題,經過大量研究,我仍然在Windows 7中存在問題(在Windows 10中可以正常工作)。 我這次可以提供一些其他有用的信息,如下所示。 注意,我也嘗試過在Crumb位置使用url編碼的AQS Search語法,並且仍然會遇到相同的運行時錯誤。 還要注意,當我省略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()
https://msdn.microsoft.com/zh-CN/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)
希望這會成為您解決方案的火花。更多命令行選項的參考
經過反復試驗和錯誤,我找到了一個解決方案,該解決方案使我的應用程序的搜索功能可以在Windows 7和Windows 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)
也許有更好的方法,但這對我有用。
感謝那些為我指出正確方向的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.