C#文件搜索小程序

来源:互联网 发布:php彩票网站系统 编辑:程序博客网 时间:2024/05/11 06:31

今天突发奇想,想做一个窗口程序,要求达到如下目标:

  1. 列出当前目录下的所有文件夹及所有文件,包含子目录及子文件。
  2. 能够找出名字包含制定字符串的文件。
  3. 删除操作。

其实以前用java已经基本实现,但是做出来的jar程序只能自己用,别人用需要安装jre或者jdk,现在一般人的电脑很少安装java的,而net.framework安装的人还是不少。所以决定用vs c#做一个同样的程序。

由于c#使用经历尚浅,权当入门练习了。

c#文件操作类

c#对文件的操作主要由FileInfo、File、DirectoryInfo和Directory来完成

要使用这四个类,首先要

using System;using System.IO;

按照我初步的理解,FileInfo和DirectoryInfo的方法是动态的,而File和Directory则可以提供静态方法。

下面记录编写过程中学到的几个比较有用的方法:

String path = System.AppDomain.CurrentDomain.BaseDirectory;//获取当前应用所在的目录
//// Summary://     Returns the subdirectories of the current directory.//// Parameters://   searchPattern://     The search string. For example, "System*" can be used to search for all directories//     that begin with the word "System".////   searchOption://     One of the enumeration values that specifies whether the search operation//     should include only the current directory or all subdirectories.//// Returns://     An array of System.IO.DirectoryInfo objects.//// Exceptions://   System.IO.DirectoryNotFoundException://     The path encapsulated in the System.IO.DirectoryInfo object is invalid, such//     as being on an unmapped drive.////   System.Security.SecurityException://     The caller does not have the required permission.////   System.UnauthorizedAccessException://     The caller does not have the required permission.     public DirectoryInfo[] GetDirectories();public DirectoryInfo[] GetDirectories(string searchPattern);public DirectoryInfo[] GetDirectories(string searchPattern, SearchOption searchOption);
//// Summary://     Returns a file list from the current directory matching the given search//     pattern and using a value to determine whether to search subdirectories.//// Parameters://   searchPattern://     The search string. For example, "System*" can be used to search for all directories//     that begin with the word "System".////   searchOption://     One of the enumeration values that specifies whether the search operation//     should include only the current directory or all subdirectories.//// Returns://     An array of type System.IO.FileInfo.//// Exceptions://   System.ArgumentException://     searchPattern contains one or more invalid characters defined by the System.IO.Path.GetInvalidPathChars()//     method.////   System.ArgumentNullException://     searchPattern is null.////   System.ArgumentOutOfRangeException://     searchOption is not a valid System.IO.SearchOption value.////   System.IO.DirectoryNotFoundException://     The path is invalid (for example, it is on an unmapped drive).////   System.Security.SecurityException://     The caller does not have the required permission.public FileInfo[] GetFiles();public FileInfo[] GetFiles(string searchPattern);public FileInfo[] GetFiles(string searchPattern, SearchOption searchOption);
//排除回收站里面的文件if (!item.FullName.Contains("$RECYCLE.BIN")){    //Code}
//调用系统程序打开文件System.Diagnostics.Process.Start("explorer.exe", flist.SelectedItem.ToString());//添加 "/select,"以后,explorer会打开文件所在目录并默认选中该文件System.Diagnostics.Process.Start("explorer.exe", "/select," + flist.SelectedItem.ToString());
//获取当前目录的父目录String fatherPath = new FileInfo(flist.SelectedItem.ToString()).DirectoryName;System.Diagnostics.Process.Start("explorer.exe", fatherPath);
//删除多个文件foreach (String itemName in flist.SelectedItems) {    FileInfo fi = new FileInfo(itemName);    fi.Delete();}
//系统确认窗口if (MessageBox.Show("确定删除?", "删除操作不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes) {}

实际效果图如下:

这里写图片描述

因为程序本身非常简单,所以难点只有这么多,搞明白以后,剩下的就是对UI的不断调整美化,以及对VS慢慢的熟悉过程。

0 0
原创粉丝点击