显示该位置已存在文件的对话框 [英] Showing the dialog of the file already existence at the location

查看:71
本文介绍了显示该位置已存在文件的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户可以在其中与数据库进行交互并准备一份他需要的报告.项目价格在数据库中列出.选择项目后,他将生成Excel格式的报告.当他单击生成报告"按钮时,将询问他将报告保存在何处.这些都很好.问题在于,当他保存报告并单击生成报告"按钮时,如果相同名称的文件存在于同一位置,则他能够在同一位置保存相同名称的相同文件.它基本上替代了旧的.我编写了一个代码来检查文件是否存在并且可以工作,但是问题是在保存文件时它不会立即出现.它出现在之前.我想要的是,当我尝试将文件保存在任何位置时,它立即提示我该文件存在,并且您要用它替换它",更像是Windows对话框,当您尝试将Word文档保存到其中时文件名称相同的位置.

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.

这是代码

  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);
        }
    }

推荐答案

您尚未提供任何代码,所以我感到奇怪.

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

但是您正在使用SaveFileDialog吗?如果没有,我强烈建议.它提供了该功能.

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也具有您可以定义的几个属性.例如用于定义可接受的文件扩展名的Filter属性.

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

这篇关于显示该位置已存在文件的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆