【问题标题】:Save file dialog confirmation保存文件对话框确认
【发布时间】:2013-08-20 15:14:57
【问题描述】:

我对制作保存文件对话框感到震惊。我已经完成了所有工作,但是当我尝试保存已经存在的文件时,我想显示一个对话框,其中包含覆盖、取消或否的选项。当用户单击否时,我希望 saveFodlerDialog 再次显示并迭代该过程。但我不知道如何实现它。

我在下面粘贴相关代码:

private void New_Project(object sender, RoutedEventArgs e)
    {

        var saveFolderDlg = new System.Windows.Forms.FolderBrowserDialog();

        System.Windows.Forms.DialogResult dlgResult = saveFolderDlg.ShowDialog();

        if (dlgResult == System.Windows.Forms.DialogResult.OK)
        {

            saveFolderDlg.RootFolder = Environment.SpecialFolder.Desktop;
            saveFolderDlg.ShowNewFolderButton = true;
            string projectPath = saveFolderDlg.SelectedPath;
            string prjFileName = System.IO.Path.GetFileName(projectPath);
            string newPath = System.IO.Path.Combine(projectPath, prjFileName);

            if (!System.IO.File.Exists(newPath+".rnd"))
            {
                CreateNewProejct(projectPath);//works fine
            }
            else
            {
                string msgBoxTxt = "Project already exists, Override?";
                MessageBoxButton button = MessageBoxButton.YesNoCancel;
                string caption = "New porject";
                MessageBoxImage icon = MessageBoxImage.Warning;
                MessageBoxResult result = MessageBox.Show(msgBoxTxt,caption, button, icon);

                switch (result)
                {
                    case MessageBoxResult.No:
                        //what to do here to restart the process of saving project
                        break;                           
                    case MessageBoxResult.Cancel:
                        break;
                    case MessageBoxResult.Yes:
                        CreateNewProejct(projectPath);
                        break;
                }
            }

        }
    }

【问题讨论】:

    标签: c# wpf .net-4.0 dialog


    【解决方案1】:

    您可以再次递归调用 NewProject 方法。

    private void NewProject()
    {
        var saveFolderDlg = new FolderBrowserDialog();
    
        DialogResult dlgResult = saveFolderDlg.ShowDialog();
    
        if (dlgResult == System.Windows.Forms.DialogResult.OK)
        {
    
            saveFolderDlg.RootFolder = Environment.SpecialFolder.Desktop;
            saveFolderDlg.ShowNewFolderButton = true;
            string projectPath = saveFolderDlg.SelectedPath;
            string prjFileName = System.IO.Path.GetFileName(projectPath);
            string newPath = System.IO.Path.Combine(projectPath, prjFileName);
    
            if (!System.IO.File.Exists(newPath + ".rnd"))
            {
                CreateNewProejct(projectPath);//works fine
            }
            else
            {
                string msgBoxTxt = "Project already exists, Override?";
                MessageBoxButton button = MessageBoxButton.YesNoCancel;
                string caption = "New porject";
                MessageBoxImage icon = MessageBoxImage.Warning;
                MessageBoxResult result = MessageBox.Show(msgBoxTxt, caption, button, icon);
    
                switch (result)
                {
                    case MessageBoxResult.No:
                        NewProject();
                        break;
                    case MessageBoxResult.Cancel:
                        break;
                    case MessageBoxResult.Yes:
                        CreateNewProejct(projectPath);
                        break;
                }
            }
        }
    }
    

    顺便说一句:您的代码中有一些拼写错误 --> Proejct :)

    【讨论】:

    • 您应该生成一个新函数,以便可以从任何地方干净地调用它,而不仅仅是一些发送者和路由事件参数。另外,我会添加一个额外的参数来设置保存路径。如果此参数为空,则路径应为桌面,否则,打开给定路径。这样,用户将获得之前选择的文件夹,您的所有用户都非常感谢 ;)
    • 好吧,我猜我注意力不集中。谢谢@BobClaerhout。我会记住的。
    • 是的,我更改了方法的签名并没有指出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多