【问题标题】:openFiledialog only the name [duplicate]openFiledialog 只有名称 [重复]
【发布时间】:2013-06-20 08:19:00
【问题描述】:

谁能告诉我如何只显示名称而不是名称前面的完整路径。 我发现此代码仅显示扩展名或仅显示不带扩展名的文件名。我想要显示的是带有扩展名的文件的名称。
像这样: 示例.txt。

代码:

private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "Binary Files (.BIN; .md6; .md7)|*.BIN; *.md6; *.md7|All Files (*.*)|*.*";


        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamReader sr = new
                System.IO.StreamReader(openFileDialog1.FileName);
                sr.Close();
            }
        }
        String filedata = openFileDialog1.FileName;

        openFileDialog1.Title = ("Choose a file");
        openFileDialog1.InitialDirectory = "C:\\Projects\\flashloader2013\\mainapplication\\Bootfiles";

        //textBox1.Text = (System.IO.Path.GetExtension(openFileDialog1.FileName));
        textBox1.Text = (System.IO.Path.Get(openFileDialog1.FileName));
    }

感谢大家的帮助

【问题讨论】:

  • Path.GetFileName 编辑你应该在你的班级顶部也包括using System.IO,这样你就不必继续写了
  • 虽然已经回答了,但是这个问题很容易被google或stackoverflow找到!下次使用搜索:-)

标签: c# openfiledialog


【解决方案1】:
var onlyfilename = Path.GetFileName(openFileDialog1.FileName);

【讨论】:

  • 我会尽可能将其标记为答案。
【解决方案2】:

【讨论】:

  • 不知道那个,谢谢
猜你喜欢
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多