简体   繁体   中英

Save file dialog confirmation

i got struck at making a save file dialog box. I got everything done, but I want to show a dialog box when I try to save file which is already existing, with options to override,cancel or no. When the user click no, I want the saveFodlerDialog to show up again and iterate the process. But I dont know how to implement it.

I am pasting relevant code below:

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

        }
    }

You could recursively call the NewProject method again.

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: there are some typos in your code --> Proejct :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM