[纯属YY]c#实现多线程删除文件 | 狂野铜匠

[纯属YY]c#实现多线程删除文件

时间:2011-5-27  |   0  |  标签:, ,

公司图片服务器某个盘今天又满了,每次都是用一个控制台程序去删文件,我DOS命令不熟,今天突发奇想用上多线程是不是会快一些呢?然后就写了些代码。(PS:可能这些代码你一辈子都用不上。)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;

namespace FiledbWrite
{
    class Program
    {
        static void Main(string[] args)
        {   
            DateTime timeStart = DateTime.Now;
            string path = System.Configuration.ConfigurationManager.AppSettings["path"];
           List allPath = new List();
            int I = 0;
            foreach (string file in Directory.GetDirectories(path))
            {
                    deleteFile d = new deleteFile();
                    d.originalDir = file;
                    d.tempDir = file;
                    d.deleteAction = new System.Threading.Thread(d.Action);
                    d.threadNO = I;
                    allPath.Add(d);
                    I++;
            }
            foreach (deleteFile d in allPath)
            {
                try
                {
                    d.deleteAction.Start();
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            while (true)
            {
                if (getState(allPath))
                    break;
            }
            DateTime timeEnd = DateTime.Now;
            TimeSpan timeSpan = timeEnd - timeStart;
            Console.WriteLine(string.Format("线程数:{0},总用时{1}s", I + 1, timeSpan.Seconds));
            Console.ReadLine();
        }
        //获取当前所有线程状态是否都已停止(删除完)
        public static bool getState(List d)
        {
            bool over = true;
            foreach (deleteFile ddd in d)
            {
                if (ddd.deleteAction.IsAlive)
                    return false;
            }
            return over;
        }
    }
    //文件删除功能类 作者头脑简单图省事 使用多个类实现了多线程
    public class deleteFile
    {
        //线程编号
        public int threadNO = 0;

        public System.Threading.Thread deleteAction;
        /// 
        /// 临时目录
        /// 
        public string tempDir = string.Empty;

        /// 
        /// 原始目录
        /// 
        public string originalDir = string.Empty;


        public void Action()
        {
            try
            {
                //删除每个文件夹下文件
                DeleteFolderFile();
                //删除文件夹
                Directory.Delete(originalDir, true);
                //停止线程
                deleteAction.Abort();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void DeleteFolderFile()
        {
            try
            {
                System.IO.File.SetAttributes(tempDir, System.IO.FileAttributes.Normal);
                foreach (string d in Directory.GetFileSystemEntries(tempDir))
                {
                    System.IO.File.SetAttributes(d, System.IO.FileAttributes.Normal);
                    if (File.Exists(d))
                    {
                        FileInfo fi = new FileInfo(d);
                        if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)//避免只读文件
                        { fi.Attributes = FileAttributes.Normal; }
                        Console.WriteLine(string.Format("当前文件:{0},当前线程:{1}", d, threadNO));
                        File.Delete(d);  //删除文件  
                    }
                    else
                    {
                        try
                        {
                            tempDir = d;
                            DeleteFolderFile();//递归
                        }
                        catch (System.Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

例子是根据要删除目录的子文件夹的个数来实例化线程的。本以为速度会快上一些,后来想了想可能达到了IO瓶颈了,服务器上文件比较多,都是图片,大小不一,看了看速度一分钟差不多能删1G。
有什么不清楚明白的地方请留言,有什么错误的地方请指正。





无觅相关文章插件,快速提升流量