一、概述
System.IO.File.Delete("要删除的文件地址和文件名");
可以用 OpenFileDialog 来让用户选择文件。
二、操作
2.1、窗体设计
主窗体中添加button按钮
2.2、代码
//删除图片
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = true; fileDialog.Title = "请选择文件";
fileDialog.Filter = "所有文件(*jpg*)|*.jpg*"; //设置要选择的文件的类型
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string localFilePath = fileDialog.FileName;//返回文件的完整路径
//string localFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "20230518_100349.jpg";//根目录下名字为20230518_100349.jpg的图片
System.IO.File.Delete(localFilePath);
MessageBox.Show("图片删除成功");
}
}
三 、演示
3.1、操作前
3.2、操作后
再次查看20230515_100611.jpg的图片已无