c#重载窗体关闭事件,遍历文件件下所有文件(及子目录),
C#中重载窗体关闭事件
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (MessageBox.Show("你确认要退出该程序吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
base.OnFormClosing(e);
else
e.Cancel = true;
}
public void button1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
folderBrowserDialog1.Description = "请选包含待清理Dwg文件的文件夹:";
try
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
textBox1.Text = folderBrowserDialog1.SelectedPath;
DirectoryInfo di = new DirectoryInfo(textBox1.Text);
//获得目录信息
FileInfo[] fiA = di.GetFiles("*.dwg", SearchOption.AllDirectories);//获得该目录下的所有文件
//di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly)
//获取目录下文件(但不包括子目录)
for (int i = 0; i < fiA.Length; i++)
{
listBox1.Items.Add(fiA[i].FullName);
}
label1.Text = "目录中共" + fiA.Length.ToString() + "个dwg文件!";
if (fiA.Length == 0)
MessageBox.Show("选择的文件夹中没有Dwg文件!");
}
}
catch(Exception ee)
{
MessageBox.Show (ee.ToString ());
}
}