C#文件搜索小程序
来源:互联网 发布:php彩票网站系统 编辑:程序博客网 时间:2024/05/11 06:31
今天突发奇想,想做一个窗口程序,要求达到如下目标:
- 列出当前目录下的所有文件夹及所有文件,包含子目录及子文件。
- 能够找出名字包含制定字符串的文件。
- 删除操作。
其实以前用java已经基本实现,但是做出来的jar程序只能自己用,别人用需要安装jre或者jdk,现在一般人的电脑很少安装java的,而net.framework安装的人还是不少。所以决定用vs 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
- C#文件搜索小程序
- C#小程序实现从百度摘取搜索结果
- c#控制台读取文件小程序
- winform写的搜索文件目录小程序
- python的一个搜索文件的小程序
- 小程序搜索不到?
- C# 文件搜索
- C#文件搜索实例
- C#文件搜索实例
- C#文件搜索代码
- C# 搜索文件目录
- C#文件搜索
- 文件搜索程序
- C# 小程序
- c#各种小程序
- C# udp小程序
- Android--快速地搜索手机文件引擎小程序(File I/O)
- 文件读写小程序
- Java:实现单链表的创建
- FTP服务基本搭建
- 61. Rotate List
- mysql主从复制
- 小知识点源码解析-STAGE由最后一个RDD确定并行度的源码解析
- C#文件搜索小程序
- Start
- android使用jre自带工具生成证书
- 《HTML+CSS基础课程》学习笔记二
- Wscript对象详解
- @date2016-3-22(软件工程男的日常)
- Python 入门 之 数据类型
- Java NIO和IO的主要区别
- 初识JSP