简单功能: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace 打开文件 { public partial class Form1 : Form { string[] LinkPath = new string[4]; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // if (Directory.Exists(comboBox1.Text)) { GetAll(ref listView1, comboBox1.Text); } else { MessageBox.Show("指定目录不存在!"); } } protected void InitLinkPath() { if (Directory.Exists(comboBox1.Text)) { LinkPath[0] = comboBox1.Text;//当前路径 LinkPath[1] = comboBox1.Text;//向后 LinkPath[2] = comboBox1.Text;//向前 if (comboBox1.Items.Contains(comboBox1.Text) || comboBox1.Items.Contains(comboBox1.Text+"//")) { LinkPath[3] = comboBox1.Text;//向上 } else { LinkPath[3] = comboBox1.Text.Substring(0, comboBox1.Text.LastIndexOf("//")); } } } /// <summary> /// 遍历当前目录 /// </summary> /// <param name="listview"></param> /// <param name="path"></param> protected void GetAll(ref ListView listview, string path) { listview.Clear(); InitLinkPath(); if (!Directory.Exists(comboBox1.Text)) { MessageBox.Show("指定目录不存在!"); return; } //获取子目录 string[] dirs = Directory.GetDirectories(path); foreach (string dir in dirs) { string str = dir.Replace(path, ""); if (str.StartsWith("//")) { str = str.Remove(0, 1); } //listview.Items.Add(dir.Replace(path, ""), 0); listview.Items.Add(str, 0); } //获取文件 string[] files = Directory.GetFiles(path); foreach (string file in files) { string str = file.Replace(path, ""); if (str.StartsWith("//")) { str = str.Remove(0, 1); } //listview.Items.Add(dir.Replace(path, ""), 0); if (str.ToLower().EndsWith(".txt")) { //如果是文本文件的话 listview.Items.Add(str, 2);//2是文本文件的图片,自己做的图片 } else { listview.Items.Add(str, 1); } } } private void listView1_DoubleClick(object sender, EventArgs e) { //MessageBox.Show(listView1.SelectedItems.Count.ToString()); if (listView1.SelectedItems.Count == 0) { return; } else { if (comboBox1.Text.EndsWith("//")) { comboBox1.Text = comboBox1.Text.Remove(comboBox1.Text.Length - 1, 1); } //如果双击的是文件夹,则进入文件夹 if (listView1.SelectedItems[0].ImageIndex == 0) { comboBox1.Text = comboBox1.Text + "//" + listView1.SelectedItems[0].Text; GetAll(ref listView1, comboBox1.Text); } else { //如果不是目录,则打开它 string fileName = comboBox1.Text + "//" + listView1.SelectedItems[0].Text; if (fileName != "" && System.IO.File.Exists(fileName)) { System.Diagnostics.Process.Start(fileName); } } } } private void Form1_Load(object sender, EventArgs e) { string[] drives = Environment.GetLogicalDrives();//获取所有的逻辑盘符 comboBox1.Items.Clear(); foreach (string dirve in drives) { comboBox1.Items.Add(dirve); } } private void button2_Click(object sender, EventArgs e) { comboBox1.Text = LinkPath[3]; GetAll(ref listView1, comboBox1.Text); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (Directory.Exists(comboBox1.Text)) { GetAll(ref listView1, comboBox1.Text); } else { MessageBox.Show("指定目录不存在!"); } } } }