0

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

        }
    }
4

1 回答 1

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 :)

于 2013-08-20T15:38:19.560 回答