[C#] Converting Relative Paths to Absolute Paths (Full Paths) (Path.GetFullPath)

When “relative paths” (e.g., ..\Data\input.csv) are specified in command line arguments or configuration files, you often need to convert them to “absolute paths” (full paths) inside the program to correctly locate the file.

Using the Path.GetFullPath method, you can resolve and convert a relative path into a complete absolute path based on the current working directory.

目次

Table of Contents

  • Implementation Example of Absolute Path Conversion
  • Sample Code
  • Execution Result (Example: Executed in C:\Work\MyApp\Bin)
  • Explanation and Technical Points
    1. The Base Location (Current Directory)
    2. Resolving “..” and “.”
    3. File Existence

Implementation Example of Absolute Path Conversion

In the following sample code, I retrieve the absolute path of a data file located in the parent directory, based on the current directory (the location where the program is running).

Sample Code

using System;
using System.IO;

public class Program
{
    public static void Main()
    {
        // 1. Relative path before conversion
        // ".." means "parent directory"
        string relativePath = @"..\Assets\texture.png";

        // (Reference) Display current working directory
        // The path is calculated based on this location
        string currentDir = Directory.GetCurrentDirectory();
        Console.WriteLine($"Current Directory: {currentDir}");
        Console.WriteLine($"Input Relative Path: {relativePath}\n");

        // 2. Convert relative path to absolute path
        // Path.GetFullPath combines the current directory and relative path,
        // resolving ".." etc. to create a clean path
        string absolutePath = Path.GetFullPath(relativePath);

        Console.WriteLine("--- Conversion Result ---");
        Console.WriteLine($"Absolute Path: {absolutePath}");
    }
}

Execution Result (Example: Executed in C:\Work\MyApp\Bin)

Current Directory: C:\Work\MyApp\Bin
Input Relative Path: ..\Assets\texture.png

--- Conversion Result ---
Absolute Path: C:\Work\MyApp\Assets\texture.png

Explanation and Technical Points

1. The Base Location (Current Directory)

Path.GetFullPath resolves paths based on Environment.CurrentDirectory (Current Directory).

Therefore, be aware that the conversion result may differ for the same program depending on where you launched it from in the command prompt.

2. Resolving “..” and “.”

This method does not simply concatenate strings; it also correctly resolves special symbols contained in the path:

  • .. (Parent Directory): Moves up one directory level to shorten the path.
  • . (Current Directory): Is ignored and removed.

3. File Existence

This method does not check if the target file actually exists. It only performs calculations while maintaining the consistency of the path string.

If you want to check if the file exists at the converted path, please use File.Exists(absolutePath) afterwards.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次