程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> c#目錄浏覽代碼

c#目錄浏覽代碼

編輯:關於C#

定義目錄浏覽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);
            }
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved