日期:2014-05-18  浏览次数:20887 次

C# 结取文件,怎么获取指定的.jpg.bmp文件,并且可以有短文件名及完整文件名?
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; //操作文件
using System.Collections; //操作动态数组


namespace seekfile
{
  public partial class Form1 : Form
  {
  //递归遍历指定目录下的所有文件,结果保存在Txt文件中
  private ArrayList al = new ArrayList();
  private int aaa = 0;

  public Form1()
  {
  InitializeComponent();
  }
  //递归获取指定目录下的所有文件
  private void GetAllDirList(string strBaseDir)
  {
  DirectoryInfo di = new DirectoryInfo(strBaseDir);
  DirectoryInfo[] diA = di.GetDirectories();

  if (aaa == 0)
  {
  FileInfo[] fis2 = di.GetFiles();  

  for (int i2 = 0; i2 < fis2.Length; i2++)
  {
  al.Add(fis2[i2].FullName);
  }
  }

  for (int i = 0; i < diA.Length; i++)
  {
  aaa++;
  al.Add(diA[i].FullName + "\t<文件夹>");
  DirectoryInfo di1 = new DirectoryInfo(diA[i].FullName);
  DirectoryInfo[] diA1 = di1.GetDirectories();
  FileInfo[] fis1 = di1.GetFiles();

  for (int ii = 0; ii < fis1.Length; ii++)
  {
  al.Add(fis1[ii].FullName);
  }
  GetAllDirList(diA[i].FullName);
  }
  }

  public void GetCurFile_List(string sourceFilePath, string destFilePath)
  {
  string str_path = sourceFilePath;
  string dest_path = destFilePath;

  string tmp = "";
  int seq = -1;

  al.Clear();
  this.GetAllDirList(str_path);

  if (File.Exists(dest_path))
  {
  File.Delete(dest_path);
  }

  StreamWriter sw = new StreamWriter(dest_path);

  //序号+文件绝对路径
  for (int i = 0; i < al.Count; i++)
  {
  seq = i + 1;
  tmp = seq.ToString() + "\t" + al[i].ToString();
  sw.WriteLine(tmp);

  /*
  tmp += seq.ToString();
  tmp += "\t";
  tmp += al[i].ToString();
  tmp += "\r\n";
  */
  }

  //sw.WriteLine(tmp); 
  sw.Flush();
  sw.Close();
  }


  private void button1_Click(object sender, EventArgs e)
  {
  string source_Dir = "D:\\picture";
  string dest_TxtPath = "D:\\文件列表.txt";
  GetCurFile_List(source_Dir, dest_TxtPath);

  MessageBox.Show("搜索结束"); 
  }

  private void Form1_Load(object sender, EventArgs e)
  {
  textBox1.Text = "";