C#获取本地磁盘的目录。

1.先创建公用类BaseClass

using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace GetDiskList
{
class BaseClass
{
public class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
[DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
public static extern int ExtractIcon(IntPtr hInst, string lpFileName, int nIndex);
[DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttribute, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint Flags);
[DllImport("User32.dll", EntryPoint = "DestroyIcon")]
public static extern int DestroyIcon(IntPtr hIcon);
[DllImport("shell32.dll")]
public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
}

#region 文件夹的复制
/// <summary>
/// 文件夹的复制
/// </summary>
/// <param Ddir="string">要复制的目的路径</param>
/// <param Sdir="string">要复制的原路径</param>
public void Files_Copy(string Ddir, string Sdir)
{
DirectoryInfo dir = new DirectoryInfo(Sdir);
string SbuDir = Ddir;
try
{
if (!dir.Exists)//判断所指的文件或文件夹是否存在
{
return;
}
DirectoryInfo dirD = dir as DirectoryInfo;//如果给定参数不是文件夹则退出
string UpDir = UpAndDown_Dir(Ddir);
if (dirD == null)//判断文件夹是否为空
{
Directory.CreateDirectory(UpDir + "\\" + dirD.Name);//如果为空,创建文件夹并退出
return;
}
else
{
Directory.CreateDirectory(UpDir + "\\" + dirD.Name);
}
SbuDir = UpDir + "\\" + dirD.Name + "\\";
FileSystemInfo[] files = dirD.GetFileSystemInfos();//获取文件夹中所有文件和文件夹
//对单个FileSystemInfo进行判断,如果是文件夹则进行递归操作
foreach (FileSystemInfo FSys in files)
{
FileInfo file = FSys as FileInfo;
if (file != null)//如果是文件的话,进行文件的复制操作
{
FileInfo SFInfo = new FileInfo(file.DirectoryName + "\\" + file.Name);//获取文件所在的原始路径
SFInfo.CopyTo(SbuDir + "\\" + file.Name, true);//将文件复制到指定的路径中
}
else
{
string pp = FSys.Name;//获取当前搜索到的文件夹名称
Files_Copy(SbuDir + FSys.ToString(), Sdir + "\\" + FSys.ToString());//如果是文件,则进行递归调用
}
}
}
catch
{
MessageBox.Show("文件制复失败。");
return;
}
}
#endregion

#region 返回上一级目录
/// <summary>
/// 返回上一级目录
/// </summary>
/// <param dir="string">目录</param>
/// <returns>返回String对象</returns>
public string UpAndDown_Dir(string dir)
{
string Change_dir = "";
Change_dir = Directory.GetParent(dir).FullName;
return Change_dir;
}
#endregion

public void listFolders(ToolStripComboBox tscb)//获取本地磁盘目录
{
string[] logicdrives = System.IO.Directory.GetLogicalDrives();
for (int i = 0; i < logicdrives.Length; i++)
{
tscb.Items.Add(logicdrives[i]);
tscb.SelectedIndex = 0;
}
}

int k = 0;
public void GOBack(ListView lv,ImageList il,string path)
{

if (AllPath.Length != 3)
{
string NewPath = AllPath.Remove(AllPath.LastIndexOf("\\")).Remove(AllPath.Remove(AllPath.LastIndexOf("\\")).LastIndexOf("\\")) + "\\";
lv.Items.Clear();
GetListViewItem(NewPath, il, lv);
AllPath = NewPath;
}
else
{
if (k == 0)
{
lv.Items.Clear();
GetListViewItem(path, il, lv);
k++;
}
}
}
public string Mpath()
{
string path=AllPath;
return path;
}
public static string AllPath = "";//---------
public void GetPath(string path, ImageList imglist, ListView lv,int ppath)//-------
{
string pp = "";
string uu = "";
if (ppath == 0)
{
if (AllPath != path)
{
lv.Items.Clear();
AllPath = path;
GetListViewItem(AllPath, imglist, lv);
}
}
else
{
uu = AllPath + path;
if (Directory.Exists(uu))
{
AllPath = AllPath + path + "\\";
pp = AllPath.Substring(0, AllPath.Length - 1);
lv.Items.Clear();
GetListViewItem(pp, imglist, lv);
}
else
{
uu = AllPath + path;
System.Diagnostics.Process.Start(uu);
}
}
}
public void GetListViewItem(string path, ImageList imglist, ListView lv)//获取指定路径下所有文件及其图标
{
lv.Items.Clear();
Win32.SHFILEINFO shfi = new Win32.SHFILEINFO();
try
{
string[] dirs = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
for (int i = 0; i < dirs.Length; i++)
{
string[] info = new string[4];
DirectoryInfo dir = new DirectoryInfo(dirs[i]);
if (dir.Name == "RECYCLER" || dir.Name == "RECYCLED" || dir.Name == "Recycled" || dir.Name == "System Volume Information")
{ }
else
{
//获得图标
Win32.SHGetFileInfo(dirs[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = dir.Name;
info[1] = "";
info[2] = "文件夹";
info[3] = dir.LastWriteTime.ToString();
ListViewItem item = new ListViewItem(info, dir.Name);
lv.Items.Add(item);
//销毁图标
Win32.DestroyIcon(shfi.hIcon);
}
}
for (int i = 0; i < files.Length; i++)
{
string[] info = new string[4];
FileInfo fi = new FileInfo(files[i]);
string Filetype = fi.Name.Substring(fi.Name.LastIndexOf(".")+1,fi.Name.Length- fi.Name.LastIndexOf(".") -1);
string newtype=Filetype.ToLower();
if (newtype == "sys" || newtype == "ini" || newtype == "bin" || newtype == "log" || newtype == "com" || newtype == "bat" || newtype == "db")
{ }
else
{


//获得图标
Win32.SHGetFileInfo(files[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(fi.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = fi.Name;
info[1] = fi.Length.ToString();
info[2] = fi.Extension.ToString();
info[3] = fi.LastWriteTime.ToString();
ListViewItem item = new ListViewItem(info, fi.Name);
lv.Items.Add(item);
//销毁图标
Win32.DestroyIcon(shfi.hIcon);
}
}
}
catch
{
}
}
}
}

2.然后创建窗体如下的源代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace GetDiskList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
BaseClass bc = new BaseClass();
private void Form1_Load(object sender, EventArgs e)
{
bc.listFolders(toolStripComboBox1);
}

private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
bc.GetPath(toolStripComboBox1.Text, imageList1, listView1, 0);
}

private void listView1_DoubleClick(object sender, EventArgs e)
{
try
{
string pp = listView1.SelectedItems[0].Text;
bc.GetPath(pp.Trim(), imageList1, listView1, 1);
}
catch
{
MessageBox.Show("无法打开此文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void toolStripButton2_Click(object sender, EventArgs e)
{
bc.GOBack(listView1, imageList1, toolStripComboBox1.Text);
}
private int T = 0;
private string path;
private static string MyPath;
private static ArrayList al = new ArrayList();
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count != 0)
{
al.Clear();
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
al.Add(bc.Mpath() + "\\" + listView1.SelectedItems[i].Text);
}
T = 0;
}
}

private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
al.Clear();
path = bc.Mpath() + "\\" + listView1.SelectedItems[0].Text;
for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
al.Add(bc.Mpath() + "\\" + listView1.SelectedItems[i].Text);
}
System.Collections.Specialized.StringCollection files = new System.Collections.Specialized.StringCollection();
files.Add(path);
Clipboard.SetFileDropList(files);
MyPath = path;
T = 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (T == 0)
{
try
{
for (int i = 0; i < al.Count; i++)
{
string name1 = al[i].ToString().Substring(al[i].ToString().LastIndexOf("\\") + 1, al[i].ToString().Length - al[i].ToString().LastIndexOf("\\") - 1);
string paths = bc.Mpath() + "\\" + name1;
if (File.Exists(al[i].ToString()))
{
if (al[i].ToString() != paths)
{
File.Move(al[i].ToString(), paths);
}
}
if (Directory.Exists(al[i].ToString()))
{
bc.Files_Copy(paths, al[i].ToString());
DirectoryInfo di = new DirectoryInfo(al[i].ToString());
di.Delete(true);
}
}
listView1.Items.Clear();
bc.GetListViewItem(bc.Mpath(), imageList1, listView1);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
try
{
for (int i = 0; i < al.Count; i++)
{
string name1 = al[i].ToString().Substring(al[i].ToString().LastIndexOf("\\") + 1, al[i].ToString().Length - al[i].ToString().LastIndexOf("\\") - 1);
string paths = bc.Mpath() + "\\" + name1;
if (File.Exists(al[i].ToString()))
{
if (al[i].ToString() != paths)
{
File.Copy(al[i].ToString(), paths, false);
}
}
if (Directory.Exists(al[i].ToString()))
{
bc.Files_Copy(paths, al[i].ToString());
}
}
listView1.Items.Clear();
bc.GetListViewItem(bc.Mpath(), imageList1, listView1);
}
catch { }
}
}

private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i = 0; i < listView1.Items.Count; i++)
{
listView1.Items[i].Selected = true;
}
}

private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string pp = listView1.SelectedItems[0].Text;
bc.GetPath(pp.Trim(), imageList1, listView1, 1);
}
catch
{
MessageBox.Show("无法打开此文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void 重命名ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count != 0)
{
listView1.SelectedItems[0].BeginEdit();
}
}

private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (listView1.SelectedItems.Count == 0)
{
MessageBox.Show("请选择文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}

for (int i = 0; i < listView1.SelectedItems.Count; i++)
{
string path = bc.Mpath() + "\\" + listView1.SelectedItems[i].Text;
if (File.GetAttributes(path).CompareTo(FileAttributes.Directory) == 0)
{
DirectoryInfo dinfo = new DirectoryInfo(path);
dinfo.Delete(true);
}
else
{
string path1 = bc.Mpath() + "\\" + listView1.SelectedItems[i].Text;
FileInfo finfo = new FileInfo(path1);
finfo.Delete();
}
}

listView1.Items.Clear();
bc.GetListViewItem(bc.Mpath(), imageList1, listView1);
}
catch
{ }
}

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
try
{
string MyPath = bc.Mpath() + "\\" + listView1.SelectedItems[0].Text;
string newFilename = e.Label;
string path2 = bc.Mpath() + "\\" + newFilename;

if (File.Exists(MyPath) && MyPath != path2)
{
File.Move(MyPath, path2);
}

if (Directory.Exists(MyPath))
{
DirectoryInfo di = new DirectoryInfo(MyPath);
di.MoveTo(path2);
}

listView1.Items.Clear();
bc.GetListViewItem(bc.Mpath(), imageList1, listView1);
}
catch { }
}
}
}