操作檔案夾的類:Directory,需要引用的命名空間:using System.IO;
例:
class Program
{
static void Main(string[] args)
{
String str=@"D:\temp";
Directory.CreateDirectory(str);//建立檔案夾
if (Directory.Exists(str))//判斷是否存在
{
Console.WriteLine("存在temp檔案夾");
}
Directory.Delete(str);//删除檔案夾
}
}
擷取檔案夾下的所有檔案:
static void Main(string[] args)
string[] files = Directory.GetFiles("D:\\360驅動大師目錄\\驅動備份目錄");
foreach (string file in files)//去除檔案的路徑
int idx = file.LastIndexOf(@"\");
string temp = file.Substring(idx + 1, file.Length - idx - 1);
Console.WriteLine(temp);
foreach (string file in files)//顯示檔案的路徑
Console.WriteLine(file);
使用FileInfo類擷取檔案資訊:
string[] files = Directory.GetFiles("D:\\360驅動大師目錄\\驅動備份目錄");
FileInfo info = new FileInfo(file);
string tmp = info.Name;
Console.WriteLine(tmp+" "+info.Length/1024);
擷取檔案夾中子目錄的名字:
string[] dirs = Directory.GetDirectories("D:\\360驅動大師目錄");
foreach (string dir in dirs)
Console.WriteLine(dir);
File檔案類。
建立檔案用:File.Create(@"D:\ite.txt");
複制檔案:File.Copy(@"D:\ite.txt", @"E:\aa.txt");
删除檔案:File.Delete;
剪貼檔案:File.More;
判斷檔案是否存在:File.Exists;
建立一個檔案并向裡面填寫内容:
FileStream fs = new FileStream(@"D:\ite.txt", FileMode.OpenOrCreate);
StreamWriter writer = new StreamWriter(fs);
writer.WriteLine("uhuirhiuheriuhiuerhiu");
writer.Close();
fs.Close();
讀取檔案的内容:
FileStream fs = new FileStream(@"D:\ite.txt", FileMode.OpenOrCreate);
StreamReader reader = new StreamReader(fs);
string line = "";
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
reader.Close();
fs.Close();
對二進制檔案的寫操作:
FileStream fs = new FileStream(@"D:\it.txt", FileMode.OpenOrCreate|FileMode.Append);//打開建立或追加
BinaryWriter writer = new BinaryWriter(fs);
writer.Write("asgdiufhdsaiuhfoiuahss");
writer.Close();
fs.Close();
對二進制檔案的讀操作:
BinaryReader reader = new BinaryReader(fs);
char[] bt = new char[fs.Length];
reader.Read(bt, 0, (int)fs.Length);
string aa = new string(bt);
Console.WriteLine(aa);
reader.Close();
例:多線程删除檔案和檔案夾(使用于大數量的檔案和檔案夾)
using System;
轉存失敗重新上傳取消using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace DeleteDir
public partial class Form1 : Form
public Form1()
InitializeComponent();
/// <summary>
/// 選擇按鈕 選擇要删除的檔案夾
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelect_Click(object sender, EventArgs e)
FolderBrowserDialog fbdDialog = new FolderBrowserDialog();//建立FolderBrowserDialog對象
if (fbdDialog.ShowDialog() == DialogResult.OK)
txtPath.Text = fbdDialog.SelectedPath;
private void btnDel_Click(object sender, EventArgs e)
DirectoryInfo dirInfo = new DirectoryInfo(txtPath.Text.Trim());
DirectoryInfo[] dirAtt = dirInfo.GetDirectories();//擷取檔案夾下所有的檔案夾
int dirAttLength = dirAtt.Length;
for (int i = 0; i < dirAttLength; i++)
Thread[] that = new Thread[dirAttLength];
DirDelete dt = new DirDelete();
dt.DicInfo = dirAtt[i];
that[i] = new Thread(dt.Delete);
that[i].Name = "th" + i;
that[i].IsBackground = true;
that[i].Start();
if (dirAtt.Length == 0)
dt.DicInfo = dirInfo;
Thread that = new Thread(dt.Delete);
that.IsBackground = true;
that.Start();
}
public class DirDelete {
public DirectoryInfo DicInfo { get; set; }
public void Delete()
DeleteFileAndDirectory(DicInfo);
private void DeleteFileAndDirectory(DirectoryInfo info)
if (info.Exists == false){return;}
DirectoryInfo[] childDir=info.GetDirectories();
foreach (DirectoryInfo newInfo in childDir)
DeleteFileAndDirectory(newInfo);
foreach (FileInfo file in info.GetFiles())
if (file.Exists)
{
file.Attributes = FileAttributes.Normal;//防止檔案受保護不能删除
file.Delete();
}
if (info.Exists)
info.Attributes = FileAttributes.Normal;
info.Delete();