保存文件对话框确认

时间:2013-08-20 15:14:58

标签: c# wpf .net-4.0 dialog

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

        }
    }

1 个答案:

答案 0 :(得分: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;
            }
        }
    }
}
Btw:你的代码中有一些拼写错误 - > Proejct :)