C# 文件查询管理器
最近刚由VB转到C# 所以想把以前用VB写过的东西重新用C#写出来练练手 因为毕竟知道了思路所以写起来也就是搞清楚C#对照VB中用神马来实现就好了 哈哈哈哈。。。。我承认我很懒- -。。。。。不说废话了 我直接贴代码一方面为了我学习积累另一方面为了造福同样跟我一样刚起步学C#的朋友们 代码层次很低 很简单 很好理解。。。。
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.IO; using System.Diagnostics; using System.Runtime.InteropServices; using System.Collections; namespace 文件文件夹搜索 { public partial class Form1 : Form { public class Ini//文件读写 { // 声明INI文件的写操作函数 WritePrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 声明INI文件的读操作函数 GetPrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath); private string sPath = null; public Ini(string path) { this.sPath = path; } public void Writue(string section, string key, string value) { // section=配置节,key=键名,value=键值,path=路径 WritePrivateProfileString(section, key, value, sPath); } public string ReadValue(string section, string key) { // 每次从ini中读取多少字节 System.Text.StringBuilder temp = new System.Text.StringBuilder(255); // section=配置节,key=键名,temp=上面,path=路径 GetPrivateProfileString(section, key, "", temp, 255, sPath); return temp.ToString(); } } public Form1() { InitializeComponent(); } public void GetFile(string path) //获取子文件夹及其文件 { try { dataGridView1.Rows.Clear(); DirectoryInfo DriFile = new DirectoryInfo(path); foreach (FileSystemInfo DF in DriFile.GetFileSystemInfos()) { dataGridView1.Rows.Add(DF.ToString(), path.ToString()); } if (dataGridView1.Rows[0].Cells["Path"].Value == null) { dataGridView1.Rows.Add(" ", path.ToString()); } } catch (Exception err) { dataGridView1.Rows.Add(" ", path.ToString()); Console.WriteLine("错误:{0}", err); } } private void Form1_Load(object sender, EventArgs e) { string Current; Current = Directory.GetCurrentDirectory();//获取当前根目录 Ini ini = new Ini(Current + "/config.ini"); if (ini.ReadValue("Setting", "num") != "") { int n = Convert.ToInt32(ini.ReadValue("Setting", "num")); for (int i = 0; i <= n; i++) { FileFromat.Add(ini.ReadValue("Fromat", i.ToString())); } foreach (string Fromat in FileFromat) { comboBox2.Items.Add(Fromat); } } #region 驱动检索 try { DriveInfo[] allDrive = DriveInfo.GetDrives(); foreach (DriveInfo Drive in allDrive) { comboBox1.Items.Add(Drive).ToString(); } comboBox1.SelectedIndex = 0; foreach (DriveInfo Drive in allDrive) { if (Drive.Name.ToString() == comboBox1.Text) { GetFile(Drive.Name.ToString()); } } } catch (Exception err) { Console.WriteLine("错误:{0}", err); } #endregion } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { GetFile(comboBox1.Text); } private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e) { string file; string path; string FilePath; file = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["FileName"].Value.ToString(); path = dataGridView1.Rows[0].Cells["Path"].Value.ToString().Trim('\\').ToString() + '\\'; FilePath = path + file ; if (File.Exists(FilePath)) { Process.Start(FilePath.Trim('\\').ToString()); } else if (Directory.Exists(FilePath)) { GetFile(FilePath); } else { } } private void button3_Click(object sender, EventArgs e) { try { string path; string [] paths = (dataGridView1.Rows[0].Cells["Path"].Value.ToString()+ '\\').Split('\\'); if (paths.Length > 2 ) { path = dataGridView1.Rows[0].Cells["Path"].Value.ToString(); GetFile(Directory.GetParent(path).ToString()); } } catch (Exception err) { Console.WriteLine("错误:{0}", err); } } List<string> FileFromat = new List<string>();//文件类型声明 private void button1_Click(object sender, EventArgs e) { if (FileFromat.Contains(textBox1.Text) == false && textBox1.Text!="") { FileFromat.Add(textBox1.Text); comboBox2.Items.Clear(); foreach (string Fromat in FileFromat) { comboBox2.Items.Add(Fromat); } } } private void button4_Click(object sender, EventArgs e) { string Current; Current = Directory.GetCurrentDirectory();//获取当前根目录 Ini ini = new Ini(Current + "/config.ini"); int n = 0; foreach (string Fromat in FileFromat) { ini.Writue("Fromat", n.ToString(), Fromat); ini.Writue ("Setting","num",n.ToString()); n++; } } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { string path = dataGridView1.Rows[0].Cells["Path"].Value.ToString(); DirectoryInfo FileFromat = new DirectoryInfo(path); dataGridView1.Rows.Clear(); foreach (FileSystemInfo File in FileFromat.GetFileSystemInfos()) { if (File.Extension.ToLower() == comboBox2.Text) { dataGridView1.Rows.Add(File.ToString(), path.ToString()); } } } } }