如何用C#编写一个应用程序来扫描文件夹下的文件??
C#遍历指定文件夹中的所有文件
DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);
//遍历文件夹
foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
this.listBox1.Items.Add(NextFolder.Name);
//遍历文件
foreach(FileInfo NextFile in TheFolder.GetFiles())
this.listBox2.Items.Add(NextFile.Name);如何获取指定目录包含的文件和子目录
DirectoryInfo.GetFiles():获取目录中(不包含子目录)的文件,返回类型为FileInfo[],支持通配符查找;
DirectoryInfo.GetDirectories():获取目录(不包含子目录)的子目录,返回类型为DirectoryInfo[],支持通配符查找;
3DirectoryInfo. GetFileSystemInfos():获取指定目录下(不包含子目录)的文件和子目录,返回类型为FileSystemInfo[],支持通配符查找;
如何获取指定文件的基本信息;FileInfo.Exists:获取指定文件是否存在;
FileInfo.Name,FileInfo.Extensioin:获取文件的名称和扩展名;
FileInfo.FullName:获取文件的全限定名称(完整路径);
FileInfo.Directory:获取文件所在目录,返回类型为DirectoryInfo;
FileInfo.DirectoryName:获取文件所在目录的路径(完整路径);
FileInfo.Length:获取文件的大小(字节数);
FileInfo.IsReadOnly:获取文件是否只读;
FileInfo.Attributes:获取或设置指定文件的属性,返回类型为FileAttributes枚举,可以是多个值的组合FileInfo.CreationTime、FileInfo.LastAccessTime、FileInfo.LastWriteTime:分别用于获取文件的创建时间、访问时间、修改时间;
C# 代码如下:
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string text = ReadTxt("D:\\test", SelectRule);
WriteTxt("D:\\a.txt", text);
Console.Read();
}private static bool SelectRule(string text)
{
return text.Length > 5 ? true : false;
}private static void WriteTxt(string path, string text)
{
using (StreamWriter sw = new StreamWriter(path))
{
sw.Write(text);
}
}private static string ReadTxt(string path, Func<string, bool> selectRule)
{
StringBuilder text = new StringBuilder();
string temp = string.Empty;
DirectoryInfo folder = new DirectoryInfo(path);
FileInfo[] files = folder.GetFiles("*.txt", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
using (StreamReader sr = file.OpenText())
{
while (sr.Peek() > 0)
{
temp = sr.ReadLine();
if (selectRule(temp))
{
text.AppendLine(temp);
}
}
}
}
return text.ToString();
}
}
}
基本达成了你的需求。 方法很原始,你可以参考。
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.Collections;
namespace 遍历工具
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int count = 0;
ArrayList temppath = new ArrayList();
ArrayList finallypath = new ArrayList();
ArrayList lastpath = new ArrayList();
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo theFolder = new DirectoryInfo(@"F:\电影");
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
//遍历文件夹
foreach (DirectoryInfo NextFolder in dirInfo)
{
FileInfo[] fileInfo = NextFolder.GetFiles();
foreach (FileInfo NextFile in fileInfo) //遍历文件
{
if (Path.GetExtension(NextFile.ToString()) == ".bmp")
{
this.listView1.Items.Add(NextFile.FullName);
temppath.Add(NextFile.FullName);
count++;
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
for(int i=0;i<count;i++)
{
string temp=temppath[i].ToString();
int len=temp.Length;
finallypath.Add (temppath[i].ToString().Substring(0, len-4));
lastpath.Add(finallypath[i]+".jpg");
}
for (int i = 0; i < count; i++)
{
File.Copy(temppath[i].ToString(),lastpath[i].ToString());
File.Delete(temppath[i].ToString());
}
MessageBox.Show("转换完成!");
}
}
}
DirectoryInfo mydir = new DirectoryInfo(dirp);
foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
{
//在这里处理转换
}