Public Static classFolderbrowserlauncher {/// <summary> ///Using the title text to look for the Top Level dialog window is fragile. ///in particular, this would fail in non-english applications. /// </summary> Const string_toplevelsearchstring ="Browse for Folder"; /// <summary> ///these should is more robust. We find the correct child controls in the dialog///by using the GetDlgItem method, rather than the FindWindow (Ex) method,///because the dialog item IDs should be constant. /// </summary> Const int_dlgitembrowsecontrol =0; Const int_dlgitemtreeview = -; [DllImport ("user32.dll", SetLastError =true)] Static externIntPtr FindWindow (stringLpclassname,stringlpwindowname); [DllImport ("user32.dll")] Static externIntPtr GetDlgItem (IntPtr hdlg,intNiddlgitem); [DllImport ("user32.dll", CharSet =CharSet.Auto)]Static externIntPtr SendMessage (IntPtr hWnd, UInt32 Msg, IntPtr WParam, IntPtr lParam); /// <summary> ///Some of the messages that the Tree View control would respond to/// </summary> Private Const intTv_first =0x1100; Private Const intTvm_selectitem = (Tv_first + One); Private Const intTvm_getnextitem = (Tv_first +Ten); Private Const intTvm_getitem = (Tv_first + A); Private Const intTvm_ensurevisible = (Tv_first + -); /// <summary> ///Constants used to identity specific items in the Tree View control/// </summary> Private Const intTvgn_root =0x0; Private Const intTvgn_next =0x1; Private Const intTvgn_child =0x4; Private Const intTvgn_firstvisible =0x5; Private Const intTvgn_nextvisible =0x6; Private Const intTvgn_caret =0x9; /// <summary> ///calling this method was identical to calling the ShowDialog method of the provided///FolderBrowserDialog, except that an attempt'll be made to scroll the Tree View///The currently selected folder visible in the dialog window. /// </summary> /// <param name= "Dlg" ></param> /// <param name= "parent" ></param> /// <returns></returns> Public StaticDialogResult Showfolderbrowser (FolderBrowserDialog dlg, IWin32Window parent =NULL) {DialogResult result=DialogResult.Cancel; intRetries =Ten; using(Timer t =NewTimer ()) {T.tick+ = (s, a) = = { if(Retries >0) { --retries; INTPTR Hwnddlg= FindWindow ((string)NULL, _toplevelsearchstring); if(Hwnddlg! =IntPtr.Zero) {IntPtr Hwndfolderctrl=GetDlgItem (Hwnddlg, _dlgitembrowsecontrol); if(Hwndfolderctrl! =IntPtr.Zero) {IntPtr Hwndtv=GetDlgItem (Hwndfolderctrl, _dlgitemtreeview); if(Hwndtv! =IntPtr.Zero) {INTPTR Item= SendMessage (Hwndtv, (UINT) Tvm_getnextitem,NewIntPtr (Tvgn_caret), IntPtr.Zero); if(Item! =IntPtr.Zero) {SendMessage (HWNDTV, Tvm_ensu Revisible, IntPtr.Zero, item); Retries=0; T.stop (); } } } } } Else { // //We failed to find the Tree View control. // //as a fall back (and this was an uberugly hack), we'll send//some fake keystrokes to the application in a attempt to force//The Tree View to scroll to the selected item. //T.stop (); Sendkeys.send ("{Tab}{tab}{down}{down}{up}{up}"); } }; T.interval=Ten; T.start (); Result=dlg. ShowDialog (parent); } returnresult; } }
C # FolderBrowserDialog Scroll to Selected Path folder selection box The initial position does not scroll to the default folder