Remove path to file from textbox when file selected C# Visual Studio

1.1k Views Asked by At

Hello I am trying to select a file from a directory from my Windows Form application, but I can't seem to find anything that will remove the path from the text box and keep only the file name (Example: "C:\Users\Users\Documents\File.txt" would be just "File.txt") where it saves the output when file is selected.

         OpenFileDialog openFileDialog1 = new OpenFileDialog();

         openFileDialog1.InitialDirectory = @"C:\OUTPUT";
         openFileDialog1.Title = "Browse exe Files";

         openFileDialog1.CheckFileExists = true;
         openFileDialog1.CheckPathExists = true;
         openFileDialog1.Filter = "exe files | *.exe";

         openFileDialog1.DefaultExt = "exe";
         openFileDialog1.FilterIndex = 2;
         openFileDialog1.RestoreDirectory = true;

         openFileDialog1.ReadOnlyChecked = true;
         openFileDialog1.ShowReadOnly = true;

         if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             textBox6.Text = openFileDialog1.FileName;
         }

Can anyone enlighten me on how to do this?

Thanks

1

There are 1 best solutions below

2
On BEST ANSWER

You can use the Path.GetFileName function for this.

 textBox6.Text = System.IO.Path.GetFileName(openFileDialog1.FileName);