openFiledialog只有这个名字

时间:2013-06-20 08:19:01

标签: c# openfiledialog

有人可以告诉我如何只显示名称而不是名称前面的完整路径。 我发现这个代码只显示扩展名或只显示没有扩展名的文件名。我想要显示的是具有扩展名的文件的名称 像这样: example.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));
    }

感谢大家的帮助

2 个答案:

答案 0 :(得分:13)

var onlyfilename = Path.GetFileName(openFileDialog1.FileName);

答案 1 :(得分:4)