Home > Net >  Get folder name of selectedpath in folderBrowserDialog
Get folder name of selectedpath in folderBrowserDialog

Time:11-23

Hi how can I display selected folder name in textbox. I have 3 Folder. It display the 2 folders in the textbox but the other one it won't display.

For example:

C:\EmpRecord\Details\Name\MiddleName\Lastname

The 2 folder name display without a problem.

For the Middlename here's the code:

middleName.Text = Path.GetFileName(Path.GetDirectoryName(folderBrowserDialog1.SelectedPath));

Lastname:

lastName.Text = new DirectoryInfo(folderBrowserDialog1.SelectedPath).Name;

For the Name it doesn't display in the textbox. How can I display it in textbox?

CodePudding user response:

You just need to call GetFileName and GetDirectoryName multiple times, e.g.

var folderPath = @"C:\EmpRecord\Details\Name\MiddleName\Lastname";

Console.WriteLine(Path.GetFileName(folderPath);

folderPath = Path.GetDirectoryName(folderPath);

Console.WriteLine(Path.GetFileName(folderPath);

folderPath = Path.GetDirectoryName(folderPath);

Console.WriteLine(Path.GetFileName(folderPath);

That will display the following:

Lastname
MiddleName
Name

CodePudding user response:

Try this:

Name.Text = folderBrowserDialog1.SelectedPath.Substring(folderBrowserDialog1.SelectedPath.LastIndexOf(@"\")   1).Substring(4));
  • Related