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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string DirPath = @"C:\temp\";
if (Directory.Exists(DirPath))
{
var files = Directory.GetFiles(DirPath);
foreach (var obj in files)
{
FileStream fs = new FileStream(DirPath + obj.Substring(obj.LastIndexOf("\\") + 1), FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
//如果檔案大小小於1000 Bytes就刪除(當然也可以設定大於多少Bytes就刪除)
if (sr.BaseStream.Length < 1000)
{
sr.Close();
File.Delete(DirPath + obj.Substring(obj.LastIndexOf("\\") + 1));
}
}
}
else
{
MessageBox.Show("Directory not found");
}
}
}
}
public void deleteFile()
{
string DirPath = @"C:\Temp";
string fileName = "", tempFile;
long size;
if (Directory.Exists(DirPath))
{
var files = Directory.GetFiles(DirPath);
foreach (var obj in files)
{
fileName = obj.Substring(obj.LastIndexOf("\\") + 1);
tempFile = DirPath + @"\" + fileName;
using (StreamReader sr = new StreamReader(tempFile, Encoding.Default))
{
size = sr.BaseStream.Length;
}
if (size <= 400)
{
File.Delete(tempFile);
}
}
}
else
{
MessageBox.Show("Error");
}
}