Showing the dialog of the file already existence at the location

那年仲夏 提交于 2020-02-05 04:51:05

问题


I have an application in which the user interacts with a database and prepares a report of what he needs. The item prices are listed in the database. Once he chooses the items then he generates a report in Excel format. When he hits the Generate a Report button, he is asked where to save the report. These are all good. The problem is that when he saves the report and hits the Generate A Report button, he is able to save the same file with the same name at the same location when a file of the same name exists at that location. It basically replaces the old one. I wrote a code to check for the file if it exists and it works but the problem is that it does not appear at the instant when you are saving the file. It appears before. What I want is when I try to save the file at any location, it gives me at that instant that "The file exists and do you want to replace it with it", more like Windows dialog when you try to save a word document into a location where there is a document with the same name.

If anyone needs any clarifications please comment, I am online most of the times.

Here is the code

  private void btnRunReport_Click(object sender, EventArgs e)
    {
        if (cmbReportsList.SelectedIndex != -1)
        {
            _qry = new QueryMgt();
            OpenProjectDb();

            string _sProjectName = (dbManager.ExecuteScalar(CommandType.Text, _qry.GetProjectFileName())).ToString();
            CloseProjectDb();
            if (cmbReportsList.SelectedIndex == 0)
            {


                SaveFile.Filter = "excel 2007 (*.xlsx)|*.xlsx";                   
                SaveFile.DefaultExt = "xlsx";
                SaveFile.FileName = _sProjectName + " Report1";
                strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);



                    if (SaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {

                        this.Text = System.IO.Path.GetFileName(SaveFile.FileName);
                        strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
                        if (strFilepath.Length > 218) { strFilepath = StringMgt.Left(strFilepath, 218); }

                        btnRunReport.Visible = false;
                        bg1804Rpt.RunWorkerAsync();
                        Application.DoEvents();
                    }




            }
            else if (cmbReportsList.SelectedIndex == 1)
            {
                SaveFile.FileName = _sProjectName + " Report2";
                SaveFile.Filter = "excel 2007 (*.xlsx)|*.xlsx"; 
                SaveFile.DefaultExt = "xlsx";

                if (SaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //SaveFile.FileName = "Report2";
                    this.Text = System.IO.Path.GetFileName(SaveFile.FileName);
                    strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
                    if (strFilepath.Length > 218) { strFilepath = StringMgt.Left(strFilepath, 218); }

                    btnRunReport.Visible = false;
                    bgMCRpt.RunWorkerAsync();
                    Application.DoEvents();
                }
            }
            else if (cmbReportsList.SelectedIndex == 2)
            {
                _qry = new QueryMgt();
                _formula = new FormulaMgt();
                string sSQL;
                string chrCountryName = "";

                OpenProjectDb();
                OpenTableDb();

                DataSet dsProjectSpec;
                DataSet dsCountry;

                sSQL = _qry.GetProjectSpec().ToString();
                dsProjectSpec = dbManager.ExecuteDataSet(CommandType.Text, sSQL);

                sSQL = _qry.GetCountry().ToString();
                dsCountry = dbManagerTable.ExecuteDataSet(CommandType.Text, sSQL);

                foreach (DataRow row in dsCountry.Tables[0].Select("pk_lngCountryID ='" + dsProjectSpec.Tables[0].Rows[0]["lngCountryId"] + "'"))
                {
                    chrCountryName = row["chrCountryName"].ToString();
                    break;
                }
                SaveFile.FileName = _sProjectName + " Report3";

                SaveFile.Filter = "excel 2007 (*.xlsx)|*.xlsx"; 
                SaveFile.DefaultExt = "xlsx";
                // SaveFile.Filter = "Microsoft Office Excel Worksheet (*.xlsx)|*.xls|All files (*.*)|*.*";
                if (SaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //SaveFile.FileName = "Report3";
                    this.Text = System.IO.Path.GetFileName(SaveFile.FileName);
                    strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
                    if (strFilepath.Length > 218) { strFilepath = StringMgt.Left(strFilepath, 218); }

                    btnRunReport.Visible = false;
                    bgMDRpt.RunWorkerAsync();
                    Application.DoEvents();
                }
            }
        }
        else if (cmbReportsList.SelectedIndex == -1)
        {
            MessageBox.Show("Please select any report", "Info", MessageBoxButtons.OK,    MessageBoxIcon.Exclamation);
        }
    }

回答1:


You have not provided any code so I am left wondering.

But are you using SaveFileDialog? If not, I highly suggest it. It provides that functionality.

SaveFileDialog sfd = new SaveFileDialog();
sfd.OverwritePrompt = true;
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    // Do Something
    // Access the filename they choose via: ofd.FileName
}

And if the user selects a file that exists, they will be asked if they are sure they want to overwrite it.

SaveFileDialog also has several properties you can define. Such as the Filter property for defining acceptable file extensions.



来源:https://stackoverflow.com/questions/12388895/showing-the-dialog-of-the-file-already-existence-at-the-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!