Hi Guys,
I am trying to display a selected file name by user(using OpenFileDialog class and FileName property) as

if (dlgOpen.ShowDialog() == DialogResult.OK)
    {
     txtFileName.Text = dlgOpen.FileName;
    }

but the problem is this return all path info which I do not need! Can you please let me know How I can get rid of the path and just retrieve or display the file name only

Thanks for your time, in advanced.

Recommended Answers

All 3 Replies

Try this...

if (dlgOpen.ShowDialog() == DialogResult.OK)

    {

     txtFileName.Text = dlgOpen.SafeFilename;

    }

...or Path.GetFileName();

using System;
using System.IO;

namespace DW_398724
{
   class Program
   {
      static void Main(string[] args)
      {
         string strFilepath = "c:/documents and settings/user/directory/sub_director/text.txt";
         Console.Write(Path.GetFileName(strFilepath)); // prints text.txt
      }
   }
}

Thank you both bhagawatshinde and thines01,
it works now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.