定義目錄浏覽dialog類:
public class MyFolderDialog : System.Windows.Forms.Design.FolderNameEditor
{
FolderNameEditor.FolderBrowser fDialog = new System.Windows.Forms.Design.FolderNameEditor.FolderBrowser();
public MyFolderDialog()
{
fDialog.Style = FolderBrowserStyles.BrowseForEverything|FolderBrowserStyles.ShowTextBox;
fDialog.StartLocation = FolderBrowserFolder.MyComputer;
}
public DialogResult DisplayDialog()
{
return DisplayDialog("請選擇一個文件夾");
}
public DialogResult DisplayDialog(string description)
{
fDialog.Description = description; return fDialog.ShowDialog();
}
public string Path
{
get
{
return fDialog.DirectoryPath;
}
}
~MyFolderDialog()
{
fDialog.Dispose();
}
}
引用System.design .dll
使用以下:
using System.Windows.Forms;
using System.Windows.Forms.Design;
使用函數:
public string GetDir()
{
MyFolderDialog folderdialog = new MyFolderDialog();
if (folderdialog.DisplayDialog() == DialogResult.OK)
{
return folderdialog.Path.ToString();
}
else
{
return string.Empty;
}
}
找某目錄下的某類的所有文件:
public void GetSubDirectoryAndFiles(string fullName ,out string[] outfiles)
{
DirectoryInfo dir = new DirectoryInfo(fullName);
DirectoryInfo[] dirSubs = dir.GetDirectorIEs();
foreach (DirectoryInfo dirSub in dirSubs)
{
if ((dirSub.Attributes & FileAttributes.Hidden) != 0)
{
continue;
}
GetSubDirectoryAndFiles(dirSub.FullName);
}
System.IO.FileInfo[] files = dir.GetFiles("*.dwg");
foreach (System.IO.FileInfo file in files)
{
outfiles.Add(file.FullName);
}
}