Link to home
Start Free TrialLog in
Avatar of dodgerfan
dodgerfanFlag for United States of America

asked on

Use substring in C# to get part of a file path

I need to get the folder name from a file path using substring in c#. The file path looks like this: O:\folder1\subfolder\text.docx. From this I want to pull out folder1 only. How can I do that using substring?
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

One possible way using GetDirectories is shown below:
            string [] subDirectories = System.IO.Directory.GetDirectories(@"C:\Program Files");
            if (subDirectories.Length > 0)
            {
                string firstSubDir = subDirectories[0];
                Console.WriteLine(firstSubDir);
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Misha
Misha
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial