c# 實現檔案瀏覽功能

來源:互聯網
上載者:User
C#的功能十分強大,用它可以輕鬆地做出屬於自己的檔案瀏覽器。下面簡單地介紹一下檔案瀏覽器的大致實現過程。其中涉及的有關這些控制項的具體用法可參見C#的線上說明。

  你需要用到幾個控制項:

   TreeView(用於顯示顯示分類樹);

   ListView(用於顯示檔案和目錄列表);

   Splitter(用於允許使用者調整TreeView和ListView的大小);

   其它的如:MainMenu,ToolBar,StatusBar,ImageList等等就看你的實際需要了。

  首先,建立一個C#項目(Windows應用程式),命名為MyFileView,將視窗命名為mainForm,調整主視窗大小(Size)。添加MainMenu,ToolBar,StatusBar,ImageList等控制項。

  然後,添加TreeView控制項,命名為treeView,Dock屬性設為Left,再添加Splitter控制項,同樣將Dock屬性設為Left。最後添加ListView控制項,命名為listView,Dock屬性設為Fill。

  介面做好了,那麼怎樣才能在這個介面裡顯示檔案夾和檔案呢?這需要我們添加代碼來實現。

  首先引用以下名字空間:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;
using System .Runtime .InteropServices ;

在mainForm_Load事件中添加以下代碼,用於在treeView控制項中顯示分類樹:
private void mainForm_Load(object sender, System.EventArgs e)
//擷取邏輯磁碟機
string[] LogicDrives=System.IO .Directory .GetLogicalDrives();
TreeNode[] cRoot =new TreeNode[LogicDrives.Length];
for (int i=0;i< LogicDrives.Length ;i++)
{
 TreeNode drivesNode=new TreeNode(LogicDrives[i]);
 treeView.Nodes .Add (drivesNode);
 if (LogicDrives[i]!="A:\\" && LogicDrives[i]!="B:\\" )
  getSubNode(drivesNode,true);
}
}

  其中,getSubNode為一方法,用於擷取子目錄,以建立分類樹節點,參數:PathName為擷取的子目錄在此節點下建立子節點,參數isEnd:結束標誌,true則結束。

 private void getSubNode(TreeNode PathName,bool isEnd)
{
 if(!isEnd)
  return; //exit this
  TreeNode curNode;
  DirectoryInfo[] subDir;
  DirectoryInfo curDir=new DirectoryInfo (PathName.FullPath);
  try
  {
   subDir=curDir.GetDirectories();
  }
  catch{}
   foreach(DirectoryInfo d in subDir)
   {
    curNode=new TreeNode(d.Name);
    PathName.Nodes .Add (curNode);
    getSubNode(curNode,false);
   }
}

 

當按一下滑鼠目錄節點左邊的+號時,節點將展開,此時,應在AfterExpand事件中加入以下代碼,以擷取此目錄下的子目錄節點:

 private void treeView_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
 try
 {
  foreach(TreeNode tn in e.Node .Nodes )
  {
   if (!tn.IsExpanded)
    getSubNode(tn,true);
   }
  }
 catch{;}
}

   當按一下滑鼠選中目錄節點時,右邊的listView控制項應顯示此目錄下的檔案和目錄,代碼如下:

 private void treeView_AfterSelect(object sender,System.Windows.Forms.TreeViewEventArgs e)
{
 listView.Items.Clear();
 DirectoryInfo selDir=new DirectoryInfo(e.Node.FullPath );
 DirectoryInfo[] listDir;
 FileInfo[] listFile;
 try
 {
  listDir=selDir.GetDirectories();
  listFile=selDir.GetFiles();
 }
 catch{}
 foreach (DirectoryInfo d in listDir)
  listView.Items .Add (d.Name,6);
 foreach (FileInfo d in listFile)
  listView.Items .Add (d.Name);
}

   至此,一個簡單的檔案瀏覽器就做成了,當然,它還很初級,甚至不能用它開啟一個檔案,加另外,它也不能顯示檔案和目錄的表徵圖,沒有進行錯誤處理,沒有進行安全控制……它能給你的只是一個思路。
轉自:

http://dotnet.chinaitlab.com/CSharp/721971_2.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.