首先我们先设计好一个界面,如下图:
代码如下:
const int SHERB_NOCONFIRMATION = 0x000001;
const int SHERB_NOPROGRESSUI = 0x000002;
const int SHERB_NOSOUND = 0x000004;
[DllImportAttribute("shell32.dll")]
private static extern int SHEmptyRecycleBin(IntPtr handle, string root, int falgs);
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK && openFileDialog1.FileName != "")
{
textBox1.Text = openFileDialog1.FileName;
}
else
{
MessageBox.Show(this ,"对不起,打开文件失败!","提示对话框",MessageBoxButtons.OK ,MessageBoxIcon.Warning );
}
}
private void button3_Click(object sender, EventArgs e)
{
SHEmptyRecycleBin(this.Handle, "", SHERB_NOCONFIRMATION + SHERB_NOPROGRESSUI + SHERB_NOSOUND);
MessageBox.Show(this, "已成功清空回收站中的文件!", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show(this, "文件路径及名称不能为空!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (!File.Exists(textBox1.Text))
{
MessageBox.Show(this, "要删除的文件不存在!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
File.Delete(textBox1.Text);
MessageBox.Show(this, "成功删除了文件!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}