简体   繁体   中英

How to get folder/directory path from input?

How do you get the last folder/directory out of user-input regardless of if the input is a path to a folder or a file? This is when the folder/file in question may not exist.

C:\\Users\\Public\\Desktop\\workspace\\page0320.xml

C:\\Users\\Public\\Desktop\\workspace

I'm trying to get out the folder "workspace" out of both examples, even if the folder "workspace" or file "page0320.xml" doesn't exist.

EDIT: Using BrokenGlass's suggestion, I got it to work.

String path = @"C:\Users\Public\Desktop\workspace";
String path2 = @"C:\Users\Public\Desktop\workspace\";
String path3 = @"C:\Users\Public\Desktop\workspace\page0320.xml";

String fileName = path.Split(new char[] { '\\' }).Last<String>().Split(new char[] { '/' }).Last<String>();

if (fileName.Contains(".") == false)
{
    path += @"\";
}

path = Path.GetDirectoryName(path);

You can substitute any of the path variables and the output will be:

C:\Users\Public\Desktop\workspace

Of course, this is working under the assuption that files have extensions. Fortunately, that assumption works for my purposes.

Thanks all. Been a long-time lurker and first-time poster. It was really impressive how fast and helpful the responses were :D

use Path.GetDirectoryName :

string path = Path.GetDirectoryName(@"C:\Users\Public\Desktop\workspace\page0320.xml");

string path2 = Path.GetDirectoryName(@"C:\Users\Public\Desktop\workspace\");

Note the trailing backslash in the path though in the second example - otherwise workspace will be interpreted as file name.

I will use DirectoryInfo in this way:

DirectoryInfo dif = new DirectoryInfo(path);
if(dif.Exist == true)
    // Now we have a true directory because DirectoryInfo can't be fooled by 
    // existing file names.
else
    // Now we have a file or directory that doesn't exist.
    // But what we do with this info? The user input could be anything
    // and we cannot assume that is a file or a directory.
    // (page0320.xml could be also the name of a directory)

You can use GetFileName after GetDiretoryName from the Path class in the System.IO namespace.

GetDiretoryName will get the path without the filename ( C:\\Users\\Public\\Desktop\\workspace ). GetFileName then returns the last part of the path as if it is a extensionless file ( workspace ).

Path.GetFileName(Path.GetDirectoryName(path));

EDIT: the path must have a trailing path separator to make this example work.

If you can make some assumptions then its pretty easy..

Assumption 1: All files will have an extension Assumption 2: The containing directory will never have an extension

If Not String.IsNullOrEmpty(Path.GetExtension(thePath))
  Return Path.GetDirectoryName(thePath)
Else
  Return Path.GetFileName(thePath)
End If

Like said before, there's not really a feasible solution but this might also do the trick:

private string GetLastFolder(string path)
{
    //split the path into pieces by the \ character
    var parts = path.Split(new[] { Path.DirectorySeparatorChar, });

    //if the last part has an extension (is a file) return the one before the last
    if(Path.HasExtension(path))
        return parts[parts.Length - 2];

    //if the path has a trailing \ return the one before the last
    if(parts.Last() == "")
        return parts[parts.Length - 2];

    //if none of the above apply, return the last element
    return parts.Last();
}

This might not be the cleanest solution but it will work. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM