【问题标题】:C# Prompt to warn user of overwriting .txt fileC# 提示警告用户覆盖 .txt 文件
【发布时间】:2016-07-13 21:16:45
【问题描述】:

不知道如何实现这一点,我没有使用 SaveFileDialog,我见过使用 OverWritePrompt = true 似乎无法让它为我工作。

我正在使用 WPF。

结构:-

我有一个名为 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;
            }
        }

然后您可以单击一个按钮,.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 :(");
            }

        }

一旦您对“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;

        }

目前一切正常,只是没有提示用户说你确定要覆盖文件。

现在可以

  1. 为此目的将一个名为 TestNote.txt 的文件加载到 文件路径框
  2. 在 textResult 中键入一些文本,甚至在单击以显示 文件
  3. 点击保存,它只会用文本 i 覆盖 TestNote.txt 刚刚进入,甚至没有警告我

希望我已经充分解释了这一点并提供了您需要的所有代码

【问题讨论】:

  • 你已经完成了所有这些,“显示对话框要求用户确认他们希望继续”的哪一部分你还在坚持?
  • 为什么不在Microsoft.Win32命名空间中使用SaveFileDialog

标签: c#


【解决方案1】:

在写入文本文件之前,只需添加一个消息框来显示您的警报消息。

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;

        }

【讨论】:

  • 一直想让我把 MessageBoxButton 放在末尾不带“S”?
【解决方案2】:

嗯,OverWritePrompt 是一个 SaveFileDialog 属性,你没有使用它:你一直在使用 File.WriteAllText(),它总是覆盖目标文件。

您想提供一个 Save 函数来在没有提示的情况下保存之前打开的文件,一个 Save As 函数会提示用户输入新文件名并在保存新文件时调用 Save As。

这是这样实现的,伪:

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

在这里,您将利用 SaveFileDialog 的功能,该功能会提示用户是否要覆盖现有文件。

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 2011-01-04
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多