公司图片服务器某个盘今天又满了,每次都是用一个控制台程序去删文件,我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);
}
}
}
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);
}
}
}
}
看了大家的评论我想说几句,关于操作系统的东西不是很了解,希望大家可以不吝赐教,贴出这段代码的主要想法是想让大家看一下代码在逻辑上,线程的使用上,语法上,有没有什么让人看起来不舒服的地方,或者是不是能用到一些设计模式来“美化”以下代码。