C# Relative Path: Complete Guide
In C#, the method commonly used to get a relative path is by using the Combine method from the Path class. This method combines the base path and the relative path to obtain the complete path. An example is shown below:
using System;
using System.IO;
class Program
{
static void Main()
{
string basePath = @"C:\Users\Public";
string relativePath = @"Documents\MyFile.txt";
string fullPath = Path.Combine(basePath, relativePath);
Console.WriteLine(fullPath);
}
}
In the example above, basePath serves as the base path and relativePath as the relative path. By using the Path.Combine method, they can be combined together to form a complete path.