前天的笔试题,但当时记不清楚这些类和方法名了,最后用了伪代码和思路。

唉,书到用时方恨少,事非经过不知难,看来古语还是有道理的。

using System.IO;

public static string pathDir = string.Empty;
public static string savePath = string.Empty;
public static StringBuilder sb = new StringBuilder();
static void Main(string[] args)
{
    pathDir = @"d:\WES 2009 相关资料\";
    savePath = @"D:\filePath.txt";
    GetDirectories(pathDir);
    //FindFile(pathDir);
    Console.ReadLine();
}

/// <summary>
/// 获取本地制定位置文件夹及所有文件
/// </summary>
/// <param name="path">路径</param>
private static void GetDirectories(string path)
{
    try
    {
        string[] fileNames = System.IO.Directory.GetFiles(path);
        string[] directories = System.IO.Directory.GetDirectories(path);
        foreach (string dir in directories)
        {
            Console.WriteLine("Directoriesname:{0}", dir);
            sb.Append(dir + "\\" + "\r\n");
            GetDirectories(dir + "\\");
        }
        foreach (string file in fileNames)
        {
            Console.WriteLine("Filename:{0}", file);
            sb.Append(file + "\r\n");
        }
        WriteText(savePath, sb.ToString());
        Console.WriteLine("All directories and files have been written into filePath.txt successfully!");
    }
    catch (Exception)
    {
        throw;
    }
}

/// <summary>
/// 递归实现查找目录下的所有子目录和文件
/// </summary>
/// <param name="dir">指定的目录</param>
public static void FindFile(string dir)                         
{
    //在指定目录及子目录下查找文件
    DirectoryInfo Dir = new DirectoryInfo(dir);
    try
    {
        foreach (DirectoryInfo d in Dir.GetDirectories())     //查找子目录  
        {
            sb.Append(Dir + d.ToString() + "\\"+ "\r\n");
            FindFile(Dir + d.ToString() + "\\");
        }
        foreach (FileInfo f in Dir.GetFiles("*.*"))             //查找文件
        {
            sb.Append(Dir + f.ToString() + "\r\n");
        }
        WriteText(savePath, sb.ToString());
        Console.WriteLine("All directories and files have been written into filePath.txt successfully!");
    }
    catch (Exception)
    {
        throw new Exception();
    }
}

/// <summary>
/// 写入文件
/// </summary>
/// <param name="savePath">save path</param>
/// <param name="dataInfo">data info</param>
public static void WriteText(string savePath, string dataInfo)
{
    try
    {
        if (!File.Exists(savePath))
        {
            System.IO.File.Create(savePath);
        }
        //Pass the filepath and filename to the StreamWriter Constructor
        StreamWriter sw = new StreamWriter(savePath);
        sw.WriteLine(dataInfo);                         //Write a line of text
        sw.Close();                                     //Close the file
    }
    catch (Exception)
    {
        throw;
    }
}

/// <summary>
/// 取本地目录内的文件名
/// </summary>
/// <param name="strPath"></param>
private void showDirFiles(string strPath)
{
    try
    {
        if (!Directory.Exists(strPath))
        {//目录不存在,创建目录  
            Directory.CreateDirectory(strPath);
        }

        DirectoryInfo mydir = new DirectoryInfo(strPath);
        FileInfo[] files = mydir.GetFiles();
        this.listBoxC.Items.Clear();//清楚listbox中现有项目  
        for (int i = 0; i < files.Length; i++)
        {
            this.listBoxC.Items.Add(files[i].ToString());
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

public void ListFiles(FileSystemInfo info)
{
    if (!info.Exists) return;

    DirectoryInfo dir = info as DirectoryInfo;
    //不是目录
    if (dir == null) return;

    FileSystemInfo[] files = dir.GetFileSystemInfos();
    for (int i = 0; i < files.Length; i++)
    {
        FileInfo file = files[i] as FileInfo;
        //是文件
        if (file != null)
        {
            //Console.WriteLine(file.FullName + "\t " + file.Length);
            if (file.FullName.Substring(file.FullName.LastIndexOf(".")) == ".jpg")
            //此处为显示JPG格式,不加IF可遍历所有格式的文件
            {
                this.list1.Items.Add(file);
                //MessageBox.Show(file.FullName.Substring(file.FullName.LastIndexOf(".")));
            }
        }
        //对于子目录,进行递归调用
        else
        {
            ListFiles(files[i]);
        }
    }
}

}

posted on 2010-04-09 14:24  GT_Andy  阅读(2974)  评论(0编辑  收藏  举报