保存文件对话框确认

本文关键字:确认 对话框 保存文件 | 更新日期: 2023-09-27 18:28:18

我在制作一个保存文件对话框时感到震惊。我已经完成了所有工作,但当我试图保存已经存在的文件时,我想显示一个对话框,其中包含覆盖、取消或否的选项。当用户单击否时,我希望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;
                }
            }
        }
    }

保存文件对话框确认

您可以再次递归调用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;
            }
        }
    }
}

顺便说一句:你的代码中有一些拼写错误-->项目:)