3

can someone tell me how to only show the name and not the full path in front of the name. I found this code put in only show the extension or only the filename without the extension. What i want to be shown is the name of the file with the extension.
Like this: example.txt.

Code:

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

Thanks all for the help

  • 1
    [Path.GetFileName](http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx) **Edit** You should include `using System.IO` at the top of your class also, to save you having to keep writing it – Sayse Jun 20 '13 at 08:21
  • Even though it already has been answered, this is a question which can very easily be found by google or stackoverflow! Use the search next time :-) – DerApe Jun 20 '13 at 08:33

2 Answers2

13
var onlyfilename = Path.GetFileName(openFileDialog1.FileName);
Marcus
  • 7,013
  • 6
  • 50
  • 79
4

have you checked out SafeFileName property? http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.safefilename.aspx

Arie
  • 4,869
  • 2
  • 28
  • 49