using VS.NET 2003,there is a FolderBrowserDialog Control in tool box .that is what you want.
我们这里所用的版本都是VS.NET 2002 version 7.0。 看来只能自己做了。 在别的地方找到一段代码,希望对大家有用。
Public Class BrowseForFolder#Region " Variables " Dim m_Path As String Dim m_Description As String Dim m_StartPath As Locations Dim m_Style As Styles #End Region#Region " Enumerations " Public Enum Locations Desktop Favorites MyComputer MyDocuments MyPictures NetAndDialUpConnections NetworkNeighborhood Printers Recent SendTo StartMenu Templates End Enum Public Enum Styles BrowseForComputer BrowseForEverything BrowseForPrinter RestrictToDomain RestrictToFilesystem RestrictToSubfolders ShowTextBox End Enum #End Region#Region " Properties " Public Property Path() As String Get Return m_Path End Get Set(ByVal Value As String) m_Path = Value End Set End Property Public Property Description() As String Get Return m_Description End Get Set(ByVal Value As String) m_Description = Value End Set End Property Public Property StartPath() As Locations Get Return m_StartPath End Get Set(ByVal Value As Locations) m_StartPath = Value End Set End Property Public Property Style() As Styles Get Return m_Style End Get Set(ByVal Value As Styles) m_Style = Value End Set End Property #End Region Public Function ShowDialog() As System.Windows.Forms.DialogResult Dim f As New FDialog(Me) Return f.ShowDialog() End Function Private Class FDialog Inherits System.Windows.Forms.Design.FolderNameEditor Private rParentClass As BrowseForFolder Public Sub New(ByVal r As BrowseForFolder) rParentClass = r End Sub Public Function ShowDialog() As System.Windows.Forms.DialogResult Dim fBFF As New System.Windows.Forms.Design.FolderNameEditor.FolderBrowser(), r As System.Windows.Forms.DialogResult fBFF.Description = rParentClass.Description Select Case rParentClass.StartPath Case Locations.Desktop fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Desktop Case Locations.Favorites fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Favorites Case Locations.MyComputer fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.MyComputer Case Locations.MyDocuments fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.MyDocuments Case Locations.MyPictures fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.MyPictures Case Locations.NetAndDialUpConnections fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.NetAndDialUpConnections Case Locations.NetworkNeighborhood fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.NetworkNeighborhood Case Locations.Printers fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Printers Case Locations.Recent fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Recent Case Locations.SendTo fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.SendTo Case Locations.StartMenu fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.StartMenu Case Locations.Templates fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Templates End Select Select Case rParentClass.Style Case Styles.BrowseForComputer fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.BrowseForComputer Case Styles.BrowseForEverything fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.BrowseForEverything Case Styles.BrowseForPrinter fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.BrowseForPrinter Case Styles.RestrictToDomain fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.RestrictToDomain Case Styles.RestrictToFilesystem fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.RestrictToFilesystem Case Styles.RestrictToSubfolders fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.RestrictToSubfolders Case Styles.ShowTextBox fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.ShowTextBox End Select r = fBFF.ShowDialog() If r = Windows.Forms.DialogResult.OK Then rParentClass.Path = fBFF.DirectoryPath End If Return r End Function End ClassEnd Class
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Windows.Forms.Design;namespace aaa { /// <summary> /// DirBrowser 的摘要说明。 /// </summary> public class DirBrowser : FolderNameEditor
看来只能自己做了。
在别的地方找到一段代码,希望对大家有用。
Dim m_Path As String
Dim m_Description As String
Dim m_StartPath As Locations
Dim m_Style As Styles
#End Region#Region " Enumerations "
Public Enum Locations
Desktop
Favorites
MyComputer
MyDocuments
MyPictures
NetAndDialUpConnections
NetworkNeighborhood
Printers
Recent
SendTo
StartMenu
Templates
End Enum Public Enum Styles
BrowseForComputer
BrowseForEverything
BrowseForPrinter
RestrictToDomain
RestrictToFilesystem
RestrictToSubfolders
ShowTextBox
End Enum
#End Region#Region " Properties "
Public Property Path() As String
Get
Return m_Path
End Get
Set(ByVal Value As String)
m_Path = Value
End Set
End Property Public Property Description() As String
Get
Return m_Description
End Get
Set(ByVal Value As String)
m_Description = Value
End Set
End Property Public Property StartPath() As Locations
Get
Return m_StartPath
End Get
Set(ByVal Value As Locations)
m_StartPath = Value
End Set
End Property Public Property Style() As Styles
Get
Return m_Style
End Get
Set(ByVal Value As Styles)
m_Style = Value
End Set
End Property
#End Region Public Function ShowDialog() As System.Windows.Forms.DialogResult
Dim f As New FDialog(Me)
Return f.ShowDialog()
End Function Private Class FDialog Inherits System.Windows.Forms.Design.FolderNameEditor Private rParentClass As BrowseForFolder Public Sub New(ByVal r As BrowseForFolder)
rParentClass = r
End Sub Public Function ShowDialog() As System.Windows.Forms.DialogResult
Dim fBFF As New System.Windows.Forms.Design.FolderNameEditor.FolderBrowser(), r As System.Windows.Forms.DialogResult fBFF.Description = rParentClass.Description Select Case rParentClass.StartPath
Case Locations.Desktop
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Desktop
Case Locations.Favorites
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Favorites
Case Locations.MyComputer
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.MyComputer
Case Locations.MyDocuments
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.MyDocuments
Case Locations.MyPictures
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.MyPictures
Case Locations.NetAndDialUpConnections
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.NetAndDialUpConnections
Case Locations.NetworkNeighborhood
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.NetworkNeighborhood
Case Locations.Printers
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Printers
Case Locations.Recent
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Recent
Case Locations.SendTo
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.SendTo
Case Locations.StartMenu
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.StartMenu
Case Locations.Templates
fBFF.StartLocation = Windows.Forms.Design.FolderNameEditor.FolderBrowserFolder.Templates
End Select Select Case rParentClass.Style
Case Styles.BrowseForComputer
fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.BrowseForComputer
Case Styles.BrowseForEverything
fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.BrowseForEverything
Case Styles.BrowseForPrinter
fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.BrowseForPrinter
Case Styles.RestrictToDomain
fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.RestrictToDomain
Case Styles.RestrictToFilesystem
fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.RestrictToFilesystem
Case Styles.RestrictToSubfolders
fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.RestrictToSubfolders
Case Styles.ShowTextBox
fBFF.Style = Windows.Forms.Design.FolderNameEditor.FolderBrowserStyles.ShowTextBox
End Select r = fBFF.ShowDialog() If r = Windows.Forms.DialogResult.OK Then
rParentClass.Path = fBFF.DirectoryPath
End If Return r End Function End ClassEnd Class
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;namespace aaa
{
/// <summary>
/// DirBrowser 的摘要说明。
/// </summary>
public class DirBrowser : FolderNameEditor
{
FolderBrowser fb = new FolderBrowser();
public string Description
{
set { _description = value; }
get { return _description; }
}
public string ReturnPath
{
get { return _returnPath; }
}
public DirBrowser() { }
public DialogResult ShowDialog()
{
fb.Description = _description;
fb.StartLocation = FolderBrowserFolder.MyComputer;
DialogResult r = fb.ShowDialog();
if (r == DialogResult.OK)
_returnPath = fb.DirectoryPath;
else
_returnPath = String.Empty;
return r;
}
private string _description = "Choose Directory";
private string _returnPath = String.Empty;
}
}
返回当前选择的目录
private void button3_Click(object sender, System.EventArgs e)
{
DirBrowser dlg = new DirBrowser();
dlg.Description = "目录选择";
if (dlg.ShowDialog() == DialogResult.OK )
MessageBox.Show(dlg.ReturnPath);
}
suosuoyyy(羊羊) 兄弟的代码很管用!!
不过有点不明白,在我的环境中都看不到FolderBrowser类,但是竟然能用?这是为什么?