2012-05-01 27 views
1

我想获得第二个到最后一级的目录树,我用数组来获取。 当它到达Console.WriteLine部分时,它不显示任何内容,它似乎跳过整行。第二个到目录树的最后一个层次

foreach (string file in files) 
{ 
    string thepathoflife = Path.GetFullPath(file); 
    string filetocopy = file; 
    string location = file; 
    bool b = false; 
    string extension = Path.GetExtension(file); 
    string thenameofdoom = Path.GetFileNameWithoutExtension(file); 
    string filename = Path.GetFileName(file); 


    //here is my attempt 
    string dirthing = Path.GetDirectoryName(filename); //here is my attempt 
    System.Console.WriteLine("" + dirthing); //here is my attempt 

回答

3

您可以拨打Path.GetDirectoryName两次走上文件夹层次:

Path.GetDirectoryName(Path.GetDirectoryName(Path.GetFullPath(file))) 

它将返回null,如果你是在层次结构太“高”。

+0

我会尝试这个 – shred1894

+0

我要查找的文件所在的文件夹,我使用的System.Console.WriteLine(“” + dirthing);看看它是否有效。 – shred1894

+0

这是有效的,它只花了我一秒钟的时间来弄清楚所有的东西都不让它显示出来,这是一个小错字,并没有导致异常 – shred1894

2

下面是几个例子:

var path = Path.GetFullPath("example.png"); 
// path == "C:\\Users\\dtb\\Desktop\\example.png" 

Path.GetFileName(path)        // "example.png" 
Path.GetFileNameWithoutExtension(path)    // "example" 
Path.GetExtension(path)        // ".png" 

Path.GetDirectoryName(Path.GetFileName(path))  // "" 

Path.GetDirectoryName(path)       // "C:\\Users\\dtb\\Desktop" 
Path.GetDirectoryName(Path.GetDirectoryName(path)) // "C:\\Users\\dtb" 
+0

事情是,我正在使用string thepathoflife = Path.GetFullPath(file);找到完整的路径。 – shred1894

+1

@ shred1894不,实际上,你不是。你得到它,然后从来没有真正使用它... – Servy

+0

我确实使用它,稍后,很久以后。我没有给出整个200多行的脚本。 – shred1894

相关问题