3

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

谢谢大家的帮助

4

2 回答 2

13
var onlyfilename = Path.GetFileName(openFileDialog1.FileName);
于 2013-06-20T08:22:06.873 回答
4

你检查过 SafeFileName 属性吗? http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.safefilename.aspx

于 2013-06-20T08:25:34.043 回答