一个菜鸟写的多线程删除文件的代码,大家给点意见

公司图片服务器某个盘今天又满了,每次都是用一个控制台程序去删文件,我DOS命令不熟,今天突发奇想用上多线程是不是会快一些呢?然后就写了些代码,本菜鸟接触c#2年多工作1年多。代码是一个小时写完的,没有任何优化,只为实现功能,希望大家对于代码给点意见,本以为用了多线程删除的速度就会快上一点,但是与原来一个线程没什么大的变化,是不是IO瓶颈了。
using  System;
using  System.Collections.Generic;
using  System.IO;
using  System.Collections;

namespace  FiledbWrite
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            DateTime timeStart 
=  DateTime.Now;
            
string  path  =  System.Configuration.ConfigurationManager.AppSettings[ " path " ];
           List
< deletefile >  allPath  =   new  List < deletefile > ();
            
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 < deletefile >  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;
        
///   <summary>
        
///  临时目录
        
///   </summary>
         public   string  tempDir  =   string .Empty;
        
///   <summary>
        
///  原始目录
        
///   </summary>
         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);
            }
        }
    }

} 

看了大家的评论我想说几句,关于操作系统的东西不是很了解,希望大家可以不吝赐教,贴出这段代码的主要想法是想让大家看一下代码在逻辑上,线程的使用上,语法上,有没有什么让人看起来不舒服的地方,或者是不是能用到一些设计模式来“美化”以下代码。 

转载于:https://www.cnblogs.com/will-WIN/archive/2011/05/27/2059812.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的保存数据文件的C++线程类示例,它使用了标准库中的线程和文件流: ``` #include <iostream> #include <fstream> #include <string> #include <thread> class DataFileWriter { public: DataFileWriter(const std::string& filename, const std::string& data) : m_filename(filename), m_data(data) {} void operator()() { std::ofstream file(m_filename); if (file.is_open()) { file << m_data; file.close(); std::cout << "Data saved to file: " << m_filename << std::endl; } else { std::cerr << "Unable to open file: " << m_filename << std::endl; } } private: std::string m_filename; std::string m_data; }; int main() { std::string filename = "data.txt"; std::string data = "Hello, World!"; DataFileWriter writer(filename, data); std::thread writer_thread(writer); writer_thread.join(); return 0; } ``` 在这个例子中,我们创建了一个名为DataFileWriter的类,它有两个私有成员变量m_filename和m_data,分别表示要保存的文件名和数据。类还重载了运算符(),这使得类的实例可以像函数一样被调用。 在运算符()的实现中,我们首先打开要保存数据的文件。如果成功打开文件,则将数据文件并关闭文件。如果无法打开文件,则输出错误消息。 在main函数中,我们创建了一个DataFileWriter的实例writer,并将它传递给一个std::thread对象writer_thread。然后我们调用join()来等待线程完成。 当运行该程序时,将会在工作目录下创建一个名为data.txt的文件,并将"Hello, World!"入该文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值