删除指定目录下的所有文件
//删除指定目录下的所有文件
private void button1_Click(object sender, EventArgs e)
{
DeleteFilesAndFolders(textBox1.Text);//路径
}
void DeleteFilesAndFolders(string path) {
//DirectoryInfo dinfo = new DirectoryInfo(path);
string[] files = Directory.GetFiles(path);
foreach(var file in files){
File.Delete(file);//文件删除
}
string[] dir = Directory.GetDirectories(path);
foreach(var d in dir){
DeleteFilesAndFolders(d);
Directory.Delete(d);//文件夹删除
}
}
更改文件扩展名
//更改文件扩展名
private void button1_Click(object sender, EventArgs e)
{
FileInfo finfo = new FileInfo(textBox1.Text);//textBox1.Text 文件路径
string strPath = textBox1.Text.Substring(0,textBox1.Text.LastIndexOf("."));//去掉扩展名
string newpath = strPath + textBox2.Text.Trim();//新的文件路径
finfo.MoveTo(newpath);
}