选择文件夹路径( FolderBrowserDialog太难用,利用OpenFileDialog来选择文件夹)

image_27196

封装类

  1  /// Wraps System.Windows.Forms.OpenFileDialog to make it present /// a vista-style dialog. ///
  2  public class FolderSelectDialog
  3  { 
  4       //Wrapped dialog 
  5          System.Windows.Forms.OpenFileDialog ofd = null;
  6      /// <summary>
  7      /// Default constructor
  8      /// </summary>
  9      public FolderSelectDialog()
 10      {
 11          ofd = new System.Windows.Forms.OpenFileDialog();
 12 
 13          ofd.Filter = "Folders|\n";
 14          ofd.AddExtension = false;
 15          ofd.CheckFileExists = false;
 16          ofd.DereferenceLinks = true;
 17          ofd.Multiselect = false;
 18      }
 19 
 20      #region Properties
 21 
 22      /// <summary>
 23      /// Gets/Sets the initial folder to be selected. A null value selects the current directory.
 24      /// </summary>
 25      public string InitialDirectory
 26      {
 27          get { return ofd.InitialDirectory; }
 28          set { ofd.InitialDirectory = value == null || value.Length == 0 ? Environment.CurrentDirectory : value; }
 29      }
 30 
 31      /// <summary>
 32      /// Gets/Sets the title to show in the dialog
 33      /// </summary>
 34      public string Title
 35      {
 36          get { return ofd.Title; }
 37          set { ofd.Title = value == null ? "Select a folder" : value; }
 38      }
 39 
 40      /// <summary>
 41      /// Gets the selected folder
 42      /// </summary>
 43      public string FileName
 44      {
 45          get { return ofd.FileName; }
 46      }
 47 
 48      #endregion
 49 
 50      #region Methods
 51 
 52      /// <summary>
 53      /// Shows the dialog
 54      /// </summary>
 55      /// <returns>True if the user presses OK else false</returns>
 56      public bool ShowDialog()
 57      {
 58          return ShowDialog(IntPtr.Zero);
 59      }
 60 
 61      /// <summary>
 62      /// Shows the dialog
 63      /// </summary>
 64      /// <param name="hWndOwner">Handle of the control to be parent</param>
 65      /// <returns>True if the user presses OK else false</returns>
 66      public bool ShowDialog(IntPtr hWndOwner)
 67      {
 68          bool flag = false;
 69 
 70          if (Environment.OSVersion.Version.Major >= 6)
 71          {
 72              var r = new Reflector("System.Windows.Forms");
 73 
 74              uint num = 0;
 75              Type typeIFileDialog = r.GetType("FileDialogNative.IFileDialog");
 76              object dialog = r.Call(ofd, "CreateVistaDialog");
 77              r.Call(ofd, "OnBeforeVistaDialog", dialog);
 78 
 79              uint options = (uint)r.CallAs(typeof(System.Windows.Forms.FileDialog), ofd, "GetOptions");
 80              options |= (uint)r.GetEnum("FileDialogNative.FOS", "FOS_PICKFOLDERS");
 81              r.CallAs(typeIFileDialog, dialog, "SetOptions", options);
 82 
 83              object pfde = r.New("FileDialog.VistaDialogEvents", ofd);
 84              object[] parameters = new object[] { pfde, num };
 85              r.CallAs2(typeIFileDialog, dialog, "Advise", parameters);
 86              num = (uint)parameters[1];
 87              try
 88              {
 89                  int num2 = (int)r.CallAs(typeIFileDialog, dialog, "Show", hWndOwner);
 90                  flag = 0 == num2;
 91              }
 92              finally
 93              {
 94                  r.CallAs(typeIFileDialog, dialog, "Unadvise", num);
 95                  GC.KeepAlive(pfde);
 96              }
 97          }
 98          else
 99          {
100              var fbd = new FolderBrowserDialog();
101              fbd.Description = this.Title;
102              fbd.SelectedPath = this.InitialDirectory;
103              fbd.ShowNewFolderButton = false;
104              if (fbd.ShowDialog(new WindowWrapper(hWndOwner)) != DialogResult.OK) return false;
105              ofd.FileName = fbd.SelectedPath;
106              flag = true;
107          }
108 
109          return flag;
110      }
111 
112      #endregion
113  }
114 
115  /// <summary>
116  /// Creates IWin32Window around an IntPtr
117  /// </summary>
118  public class WindowWrapper : System.Windows.Forms.IWin32Window
119  {
120      /// <summary>
121      /// Constructor
122      /// </summary>
123      /// <param name="handle">Handle to wrap</param>
124      public WindowWrapper(IntPtr handle)
125      {
126          _hwnd = handle;
127      }
128 
129      /// <summary>
130      /// Original ptr
131      /// </summary>
132      public IntPtr Handle
133      {
134          get { return _hwnd; }
135      }
136 
137      private IntPtr _hwnd;
138  }
139 
140  /// <summary>
141  /// This class is from the Front-End for Dosbox and is used to present a 'vista' dialog box to select folders.
142  /// Being able to use a vista style dialog box to select folders is much better then using the shell folder browser.
143  /// http://code.google.com/p/fed/
144  ///
145  /// Example:
146  /// var r = new Reflector("System.Windows.Forms");
147  /// </summary>
148  public class Reflector
149  {
150      #region variables
151 
152      string m_ns;
153      Assembly m_asmb;
154 
155      #endregion
156 
157      #region Constructors
158 
159      /// <summary>
160      /// Constructor
161      /// </summary>
162      /// <param name="ns">The namespace containing types to be used</param>
163      public Reflector(string ns)
164          : this(ns, ns)
165      { }
166 
167      /// <summary>
168      /// Constructor
169      /// </summary>
170      /// <param name="an">A specific assembly name (used if the assembly name does not tie exactly with the namespace)</param>
171      /// <param name="ns">The namespace containing types to be used</param>
172      public Reflector(string an, string ns)
173      {
174          m_ns = ns;
175          m_asmb = null;
176          foreach (AssemblyName aN in Assembly.GetExecutingAssembly().GetReferencedAssemblies())
177          {
178              if (aN.FullName.StartsWith(an))
179              {
180                  m_asmb = Assembly.Load(aN);
181                  break;
182              }
183          }
184      }
185 
186      #endregion
187 
188      #region Methods
189 
190      /// <summary>
191      /// Return a Type instance for a type 'typeName'
192      /// </summary>
193      /// <param name="typeName">The name of the type</param>
194      /// <returns>A type instance</returns>
195      public Type GetType(string typeName)
196      {
197          Type type = null;
198          string[] names = typeName.Split('.');
199 
200          if (names.Length > 0)
201              type = m_asmb.GetType(m_ns + "." + names[0]);
202 
203          for (int i = 1; i < names.Length; ++i)
204          {
205              type = type.GetNestedType(names[i], BindingFlags.NonPublic);
206          }
207          return type;
208      }
209 
210      /// <summary>
211      /// Create a new object of a named type passing along any params
212      /// </summary>
213      /// <param name="name">The name of the type to create</param>
214      /// <param name="parameters"></param>
215      /// <returns>An instantiated type</returns>
216      public object New(string name, params object[] parameters)
217      {
218          Type type = GetType(name);
219 
220          ConstructorInfo[] ctorInfos = type.GetConstructors();
221          foreach (ConstructorInfo ci in ctorInfos)
222          {
223              try
224              {
225                  return ci.Invoke(parameters);
226              }
227              catch { }
228          }
229 
230          return null;
231      }
232 
233      /// <summary>
234      /// Calls method 'func' on object 'obj' passing parameters 'parameters'
235      /// </summary>
236      /// <param name="obj">The object on which to excute function 'func'</param>
237      /// <param name="func">The function to execute</param>
238      /// <param name="parameters">The parameters to pass to function 'func'</param>
239      /// <returns>The result of the function invocation</returns>
240      public object Call(object obj, string func, params object[] parameters)
241      {
242          return Call2(obj, func, parameters);
243      }
244 
245      /// <summary>
246      /// Calls method 'func' on object 'obj' passing parameters 'parameters'
247      /// </summary>
248      /// <param name="obj">The object on which to excute function 'func'</param>
249      /// <param name="func">The function to execute</param>
250      /// <param name="parameters">The parameters to pass to function 'func'</param>
251      /// <returns>The result of the function invocation</returns>
252      public object Call2(object obj, string func, object[] parameters)
253      {
254          return CallAs2(obj.GetType(), obj, func, parameters);
255      }
256 
257      /// <summary>
258      /// Calls method 'func' on object 'obj' which is of type 'type' passing parameters 'parameters'
259      /// </summary>
260      /// <param name="type">The type of 'obj'</param>
261      /// <param name="obj">The object on which to excute function 'func'</param>
262      /// <param name="func">The function to execute</param>
263      /// <param name="parameters">The parameters to pass to function 'func'</param>
264      /// <returns>The result of the function invocation</returns>
265      public object CallAs(Type type, object obj, string func, params object[] parameters)
266      {
267          return CallAs2(type, obj, func, parameters);
268      }
269 
270      /// <summary>
271      /// Calls method 'func' on object 'obj' which is of type 'type' passing parameters 'parameters'
272      /// </summary>
273      /// <param name="type">The type of 'obj'</param>
274      /// <param name="obj">The object on which to excute function 'func'</param>
275      /// <param name="func">The function to execute</param>
276      /// <param name="parameters">The parameters to pass to function 'func'</param>
277      /// <returns>The result of the function invocation</returns>
278      public object CallAs2(Type type, object obj, string func, object[] parameters)
279      {
280          MethodInfo methInfo = type.GetMethod(func, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
281          return methInfo.Invoke(obj, parameters);
282      }
283 
284      /// <summary>
285      /// Returns the value of property 'prop' of object 'obj'
286      /// </summary>
287      /// <param name="obj">The object containing 'prop'</param>
288      /// <param name="prop">The property name</param>
289      /// <returns>The property value</returns>
290      public object Get(object obj, string prop)
291      {
292          return GetAs(obj.GetType(), obj, prop);
293      }
294 
295      /// <summary>
296      /// Returns the value of property 'prop' of object 'obj' which has type 'type'
297      /// </summary>
298      /// <param name="type">The type of 'obj'</param>
299      /// <param name="obj">The object containing 'prop'</param>
300      /// <param name="prop">The property name</param>
301      /// <returns>The property value</returns>
302      public object GetAs(Type type, object obj, string prop)
303      {
304          PropertyInfo propInfo = type.GetProperty(prop, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
305          return propInfo.GetValue(obj, null);
306      }
307 
308      /// <summary>
309      /// Returns an enum value
310      /// </summary>
311      /// <param name="typeName">The name of enum type</param>
312      /// <param name="name">The name of the value</param>
313      /// <returns>The enum value</returns>
314      public object GetEnum(string typeName, string name)
315      {
316          Type type = GetType(typeName);
317          FieldInfo fieldInfo = type.GetField(name);
318          return fieldInfo.GetValue(null);
319      }
320 
321      #endregion
322 
323  }
324  //复制到项目中即可使用

使用

  FolderSelectDialog folderSelectDialog = new FolderSelectDialog();
  if (folderSelectDialog.ShowDialog(this.Handle))
  {
      MessageBox.Show(folderSelectDialog.FileName);
  }

 

posted @ 2025-12-23 13:58  家煜宝宝  阅读(0)  评论(0)    收藏  举报