5

For example,

string path = @"C:\User\Desktop\Drop\images\";

I need to get only @"C:\User\Desktop\Drop\

Is there any easy way of doing this?

4

7 回答 7

11

You can use the Path and Directory classes:

DirectoryInfo parentDir = Directory.GetParent(Path.GetDirectoryName(path));
string parent = parentDir.FullName; 

Note that you would get a different result if the path doesn't end with the directory-separator char \. Then images would be understood as filename and not as directory.

You can also use a subsequent call of Path.GetDirectoryName

string parent = Path.GetDirectoryName(Path.GetDirectoryName(path));

This behaviour is documented here:

Because the returned path does not include the DirectorySeparatorChar or AltDirectorySeparatorChar, passing the returned path back into the GetDirectoryName method will result in the truncation of one folder level per subsequent call on the result string. For example, passing the path "C:\Directory\SubDirectory\test.txt" into the GetDirectoryName method will return "C:\Directory\SubDirectory". Passing that string, "C:\Directory\SubDirectory", into GetDirectoryName will result in "C:\Directory".

于 2012-12-18T12:45:32.267 回答
1
var parent = ""; 
If(path.EndsWith(System.IO.Path.DirectorySeparatorChar) || path.EndsWith(System.IO.Path.AltDirectorySeparatorChar))
{
  parent = Path.GetDirectoryName(Path.GetDirectoryName(path));
  parent = Directory.GetParent(Path.GetDirectoryName(path)).FullName;
}
else
  parent = Path.GetDirectoryName(path);

As i commented GetDirectoryName is self collapsing it returns path without tralling slash - allowing to get next directory.Using Directory.GetParent for then clouse is also valid.

于 2012-12-18T13:08:12.253 回答
1

Short Answer :)

path = Directory.GetParent(Directory.GetParent(path)).ToString();
于 2015-10-19T12:04:24.090 回答
0

This will return "C:\User\Desktop\Drop\" e.g. everything but the last subdir

string path = @"C:\User\Desktop\Drop\images";
string sub = path.Substring(0, path.LastIndexOf(@"\") + 1);

Another solution if you have a trailing slash:

string path = @"C:\User\Desktop\Drop\images\";
var splitedPath = path.Split('\\');
var output = String.Join(@"\", splitedPath.Take(splitedPath.Length - 2));
于 2012-12-18T12:43:58.237 回答
0

Example on the bottom of the page probably will help: http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname(v=vs.110).aspx

于 2012-12-18T12:45:20.533 回答
0
using System;

namespace Programs
{
    public class Program
    {      
        public static void Main(string[] args)
        {
            string inputText = @"C:\User\Desktop\Drop\images\";
            Console.WriteLine(inputText.Substring(0, 21));
        }
    }
}

Output:

C:\User\Desktop\Drop\

于 2012-12-18T12:45:28.647 回答
0

There is probably some simple way to do this using the File or Path classes, but you could also solve it by doing something like this (Note: not tested):

string fullPath = "C:\User\Desktop\Drop\images\";
string[] allDirs = fullPath.split(System.IO.Path.PathSeparator);

string lastDir = allDirs[(allDirs.length - 1)];
string secondToLastDir= allDirs[(allDirs.length - 2)];
// etc...
于 2012-12-18T12:49:52.193 回答