简体   繁体   English

C#提示警告用户覆盖.txt文件

[英]C# Prompt to warn user of overwriting .txt file

Not sure how to implement this, i am not using SaveFileDialog which i have seen uses OverWritePrompt = true cant seem to get that to work for me. 不知道如何实现这一点,我没有使用SaveFileDialog,我已经看到它使用OverWritePrompt = true似乎无法为我工作。

I am using WPF. 我正在使用WPF。

The structure:- 结构:-

I have a textBox called filePathBox - This contains a file path used from opening an: OpenFileDialog 我有一个名为filePathBox的文本框-它包含打开以下文件所使用的文件路径:OpenFileDialog

private void fileBrowser_Click(object sender, RoutedEventArgs e)
        {

            //Firstly creating the OpenFileDialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            //Setting the filter for the file extension of the files as well as the default extension
            dlg.DefaultExt = ".txt";
            dlg.Filter = "All Files|*.*";

            //Display the dialog box by calling the ShowDialog method
            Nullable<bool> result = dlg.ShowDialog();

            //Grab the file you selected and display it in filePathBox
            if (result == true)
            {
                //Open The document
                string filename = dlg.FileName;
                filePathBox.Text = filename;
            }
        }

You can then click a button and the .txt file displays in a textBox called textResult 然后,您可以单击一个按钮,.txt文件显示在名为textResult的文本框中。

private void helpfulNotes_Click(object sender, RoutedEventArgs e)
        {


            if (File.Exists(filePathBox.Text) && System.IO.Path.GetExtension(filePathBox.Text).ToLower() == ".txt")
            {
                textResult.Text = File.ReadAllText(filePathBox.Text);
            }

            if (string.IsNullOrWhiteSpace(filePathBox.Text))
            {
                MessageBox.Show("Please choose a file by clicking on the folder Icon :(");
            }

        }

Once you have made changes to that text in 'textResult' i have a button to save the text back to the file path that was originally loaded using the OpenFileDialog 在'textResult'中对该文本进行更改后,我有一个按钮来将文本保存回最初使用OpenFileDialog加载的文件路径

private void saveText_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(textResult.Text))
            {
                saveText.IsEnabled = false;
                MessageBox.Show("No Text to save!");
            }
            else
            {
                saveText.IsEnabled = true;

                string test = textResult.Text;
                System.IO.File.WriteAllText(filePathBox.Text, test);
            }

            //fileSaveIcon.Visibility = Visibility.Visible;
            //fileChangedIcon.Visibility = Visibility.Hidden;

        }

At the moment it all saves fine, only it doesn't prompt the user saying are you sure you want to overwrite the file. 目前,这一切都可以保存,但它不会提示用户您确定要覆盖文件。

At the moment i could 此刻我可以

  1. load a file for the purpose of this named TestNote.txt into the filePathBox 为此目的将一个名为TestNote.txt的文件加载到filePathBox中
  2. Type some text in textResult before even clicking to display the file 在单击甚至显示文件之前,在textResult中键入一些文本
  3. Click save and it would just overwrite TestNote.txt with the text i just entered without even warning me 单击保存,它只会用我刚刚输入的文本覆盖TestNote.txt,甚至不会警告我

Hopefully i have explained this adequately and provided all the code you need 希望我已经对此进行了充分的解释,并提供了您需要的所有代码

Just add a messagebox to show your alert message before writing to the text file. 在写入文本文件之前,只需添加一个消息框以显示您的警报消息。

private void saveText_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(textResult.Text))
            {
                saveText.IsEnabled = false;
                MessageBox.Show("No Text to save!");
            }
            else
            {
        if(MessageBox.Show("are you sure you want to overwrite the file.", "Alert", MessageBoxButtons.YesNo)==DialogResult.Yes)
        {
                saveText.IsEnabled = true;
                string test = textResult.Text;
                System.IO.File.WriteAllText(filePathBox.Text, test);
                }
            }

            //fileSaveIcon.Visibility = Visibility.Visible;
            //fileChangedIcon.Visibility = Visibility.Hidden;

        }

Well, OverWritePrompt is a SaveFileDialog property, which you're not using: you're always using File.WriteAllText() , which always overwrites the target file. 好吧, OverWritePrompt是一个SaveFileDialog属性,您不会使用它:您始终使用File.WriteAllText() ,该File.WriteAllText()始终会覆盖目标文件。

You want to provide a Save function that saves an earlier opened file without prompt, a Save As function that prompts the user for a new filename and also call Save As when saving a new file. 您要提供一个保存功能,该功能可以保存以前打开的文件而没有提示,另存为功能可以提示用户输入新文件名,并在保存新文件时调用另存为。

This is implemented like this, pseudo: 这样实现,伪:

private string _currentlyOpenedFile;

public void FileOpen_Click(...)
{
    var openFileDialog = new ...OpenFileDialog();

    if (openFileDialog.ShowDialog())
    {
        // Save the filename when opening a file.
        _currentlyOpenedFile = openFileDialog.FileName;
    }
}

public void FileNew_Click(...)
{
    // Clear the filename when closing a file or making a new file.
    _currentlyOpenedFile = null;
}

public void FileSave_Click(...)
{
    if (_currentlyOpenedFile == null)
    {
        // New file, treat as SaveAs
        FileSaveAs_Click();
        return;
    }
}

public void FileSaveAs_Click(...)
{
    var saveFileDialog = new ...SaveFileDialog();

    if (openFileDialog.ShowDialog())
    {
        // Write the file.
        File.WriteAllText(text, openFileDialog.FileName);

        // Save the filename after writing the file.
        _currentlyOpenedFile = openFileDialog.FileName;
    }
}

Here you'll be leveraging the SaveFileDialog's functionality which prompts the user whether they want to overwrite an already existing file. 在这里,您将利用SaveFileDialog的功能来提示用户是否要覆盖已经存在的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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