【问题标题】:How to exclude the path of the file of the SaveFileDialog box in Winform? [duplicate]Winform中如何排除SaveFileDialog的文件路径? [复制]
【发布时间】:2018-10-06 21:21:04
【问题描述】:

我正在使用 C# 在 Winform 中构建一个记事本演示应用程序。
当我单击“保存”或“另存为”按钮时,我想将表单的标题更改为文件名,不包括文件名的路径。

(例如“Demo.txt”,但不是“D:\Demo.txt”)

private string fileName;
private void mnuSaveAs_Click(object sender, EventArgs e)
    {
        SaveFileDialog dlg = new SaveFileDialog();
        dlg.Filter = "Text Documents(*.txt)|*.txt|All Files(*.*)|*.*";

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            fileName = dlg.FileName;
            StreamWriter sw = new StreamWriter(fileName);
            sw.Write(txtMain.Text);
            sw.Close();
        }

        this.Text = dlg.FileName;                             
    }

在上面的代码中,dlg.FileName 返回文件名的完整路径。
在 OpenFileDialog 中,dlg.SafeFileName 只返回文件名。但是 SaveFileDialog 没有那个属性。
如何在 SaveFileDialog 中只获取文件名?

【问题讨论】:

    标签: c# winforms savefiledialog


    【解决方案1】:

    使用来自System.IOPath.GetFileName()方法:

    Path.GetFileName(dlg.FileName);
    

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 2016-01-28
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多