21
2012
05

C# 删除选中的文本文件

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.CurrentRow != null)
                {
                    DialogResult ds = MessageBox.Show("确定要删除该文件吗?", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (ds == DialogResult.Yes)
                    {
                        int Crows = dataGridView1.CurrentRow.Index;
                        string fileName = dataGridView1.Rows[Crows].Cells["FileName"].Value.ToString();
                        DeleteFile(fileName);
                        rTxt.Text = "";
                        Form3_Load(sender, e);
                    }
                }
            }
        }

        private void DeleteFile(string fileName)
        {
            string filePath = Application.StartupPath + @"\File" + "\\" + fileName;
            try
            {
                if (!File.Exists(filePath))
                {
                    MessageBox.Show("不存在该文件!");
                }
                else
                {
                    File.Delete(filePath);
                    MessageBox.Show("删除成功!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("新增错误!请重试!错误信息:" + ex.Message);
                return;
            }
        }

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。