[.NET]取得路徑的上層路徑

[.NET]取得路徑的上層路徑

今天在看程式時,發現在取得某個路徑的上層路徑時是使用 \..\ 來串接,直接用範例來說明比較清楚。

假設Application.ExecutablePath的Paht為F:\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.EXE

使用 \..\ 來串接的程式如下,

System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"\..\";

所以組出來的路徑字串如下,

F:\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\..\

 

另外,也可透過 Directory.GetParent 來取得上層的路徑,程式如下,

System.IO.Directory.GetParent(System.IO.Path.GetDirectoryName(Application.ExecutablePath)).FullName;

 

所以路徑字串如下,

F:\WindowsFormsApplication1\WindowsFormsApplication1\bin

 

用起來有點雜,所以我們可將這些包成Extension Method,程式如下,

public static class stringExtends
{

    /// <summary>
    /// 取得某目錄的上幾層的目錄路徑
    /// </summary>
    /// <param name="folderPath">目錄路徑</param>
    /// <param name="levels">要往上幾層</param>
    /// <returns></returns>
    public static string GetParentDirectoryPath(this string folderPath, int levels)
    {
        string result = folderPath;
        for (int i = 0; i < levels; i++)
        {
            if (Directory.GetParent(result) != null)
            {
                 result = Directory.GetParent(result).FullName;
            }
            else
            {
                return result;
            }        
        }
        return result;
    }

    /// <summary>
    /// 取得某目錄的上層的目錄路徑
    /// </summary>
    /// <param name="folderPath">目錄路徑</param>
    /// <returns></returns>
    public static string GetParentDirectoryPath(this string folderPath)
    {
        return GetParentDirectoryPath(folderPath, 1);
    }

    /// <summary>
    /// 取得路徑的目錄路徑
    /// </summary>
    /// <param name="filePath">路徑</param>
    /// <returns></returns>
    public static string GetDirectoryPath(this string filePath)
    {
        return Path.GetDirectoryName(filePath);
    }
}

 

所以執行時,就比較簡潔一點,程式如下,

Application.ExecutablePath.GetDirectoryPath().GetParentDirectoryPath();
//F:\WindowsFormsApplication1\WindowsFormsApplication1\bin

//也可以往上取2層
Application.ExecutablePath.GetDirectoryPath().GetParentDirectoryPath(2);
//F:\WindowsFormsApplication1\WindowsFormsApplication1

 

補上處理往上取太多層時,會發生錯誤的處理。 謝謝Allen Kuo老師! 

 

包裝成Helper類別

using System;
using System.IO;

public class PathHelper
{
    /// 
    /// 取得某目錄的上幾層的目錄路徑
    /// 
    /// 
    /// 
    /// 
    public static string GetParentDirectoryPath(string folderPath, int levels)
    {
        string result = folderPath;
        for (int i = 0; i < levels; i++)
        {
            if (Directory.GetParent(result) != null)
            {
                result = Directory.GetParent(result).FullName;
            }
            else
            {
                return result;
            }
        }
        return result;
    }

    /// 
    /// 取得某目錄的上層的目錄路徑
    /// 
    /// 
    /// 
    public static string GetParentDirectoryPath(string folderPath)
    {
        return GetParentDirectoryPath(folderPath, 1);
    }

    /// 
    /// 取得路徑的目錄路徑
    /// 
    /// 
    /// 
    public static string GetDirectoryPath(string filePath)
    {
        return Path.GetDirectoryName(filePath);
    }
}

使用方式

 

PathHelper.GetParentDirectoryPath(Application.ExecutablePath, 2);
//F:\WindowsFormsApplication1\WindowsFormsApplication1

 

 

 

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^