openFiledialog only the name

13,592

Solution 1

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

Solution 2

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

Share:
13,592
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

    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