string path = "C:\folder1\folder2\file.txt";
What objects or methods could I use that would give me the result folder2?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would probably use something like:
The inner call to
GetDirectoryNamewill return the full path, while the outer call toGetFileName()will return the last path component – which will be the folder name.This approach works whether or not the path actually exists. This approach, does however, rely on the path initially ending in a filename. If it’s unknown whether the path ends in a filename or folder name – then it requires that you check the actual path to see if a file/folder exists at the location first. In that case, Dan Dimitru’s answer may be more appropriate.