Framework / IO (File Systems) / Paths
In This Topic
    Paths
    In This Topic

    The NPath class provides methods for working with file and folder paths and names using the file path notation of a specific File System. To work with file names in a platform independent way, use the NFileRUri class. Notably there are two types of path notations:

    The NPath.Current static property exposes the path notation suitable for the current local file system (i.e. is a shortcut to the path of the current file system). Additionally each NFileSystem object exposes an instance of the path that it uses via its Path property. The follow example performs a simple path combination operation:

    NPath example
    Copy Code
    string currentFolder = NFileSystem.Current.CurrentFolderPath;
    string fileInCurrentFolder = NPath.Current.Combine(currentFolder, "MyFile.txt");
    

    These functionality of NPath can be divided into the following categories:

     Path Manipulation 

    The following methods lets you manipulate and work with file and directory paths:

    Method Description
    string Combine(params string[] paths) combines one or more paths into one. This is most commonly used to combine an absolute base path with a relative other path, for example combining "D:\Movies" with "Terminator.avi" will result in the path "D:\Movies\Terminator.avi".
    string Normalize(string path) Normalizes the given path by resolving all "." and ".." path segments. The directory separator character for the current platform is used, i.e. '\' for Windows and '/' for Mac. For example, normalizing "C:\Documents\..\MyFile.txt" will result in "C:\MyFile.txt".
    bool IsPathRooted(string path) Checks whether the specified path string contains a root, for example "C:\Documents\MyFile.txt" contains a root - "C:\" and an Unix (Mac OS) path "/Users/John/Documents/MyFile.txt" contains a root "/".
    string GetPathRoot(string path) Gets the root directory information of the specified path (e.g. "C:\").
    string MakeRelative(string fromPath, string toPath) Gets a relative path leading from the first path (fromPath) to the second one (toPath). For example, if you pass "C:\Documents\Mark" as fromPath and "C:\Documents\Jane" as toPath, the resulting relative path will be "..\Jane".
    void SplitPath(string path, out string[] steps, out bool isRooted) Gets the steps in this path. Also retrieves whether the path is rooted.
    string JoinPath(string[] steps) Joins the specified path steps into a single path string.
     File Paths

    The following methods of the NPath static class are related to file paths:

    Method Description
    string GetExtension(string fileName) Gets the extension of the specified file name without the dot, for example: "txt", "rtf", "docx", etc. If the given file name does not have an extension, String.Empty is returned.
    string GetFileName(string path) Gets only the file name and the extension of the given path. Also works for directories. For example, if the path is "C:\Documents\MyFile.txt", this method returns "MyFile.txt". If the path it "C:\Documents", this method returns "Documents".
    string GetFileNameWithoutExtension(string path) Gets only the file name of the given full file path without the file extension. For example, if the path is "C:\Documents\MyFile.txt", this method returns "MyFile".
    string GetFullPath(string path) Returns the absolute path for the specified path string using the current directory if the given path string is relative.
           
     Folder Paths

    The following methods the NPath class are related to directory paths:

    Method Description
    string GetParentFolderPath(string path) Gets the path to the parent folder of the given path. For example, if the path is "C:\Documents\MyFile.txt", this method returns "C:\Documents\".
    string GetParentFolderName(string path) Gets the name of the parent folder of the given path. For example, if the path is "C:\Documents\MyFile.txt", this method returns "Documents".
           
    See Also