Introduction
When doing file path processing, often need to operate on the relative path of a path, then how to flatten the relative path to generate a new absolute path?
Path.Combine () method
We know that System.IO.Path is a static class designed to handle paths, and it has a combine () method that is used to splice paths, and we test the stitching effect.
We used a command-line program to test a series of relative paths relative to the file c:abc123avatar.html, and the test code was as follows:
Class Program
{
static string path = @ "c:abc123avatar.html";
static void Main (string[] args)
{
Console.WriteLine (path);
Console.WriteLine ("Enter a relative path to complete the merge:");
Console.WriteLine ();
while (true)
{
Console.WriteLine ("Merge as:" + Merge Path (Console.ReadLine ()));
Console.WriteLine ();
}
}
private static string merge path (string p)
{
Return Path.Combine (Path.getdirectoryname (Path), p);
}
}
The "Merge Path" method has the ability to first get the directory where the file is located, and then flatten it with the relative path.
Test results:
As you can see, the general path flattening is fine, but enter "..." is not handled properly as a superior directory, but directly to the merge, which is not what I expected to see.
What to do to support "..." What is the relative path of the form?
Using the constructor of a URI object
I found that the URI object can be constructed with a URI and a relative path to be constructed as a new URI, and we can represent the local file path in the form of "file://...", let's change the code to do a split test on the relative URI.
Changed code:
Class Program
{
static string path = @ "c:abc123avatar.html";
static string path = @ "file:///C:/abc/123/avatar.html";
static void Main (string[] args)
{
Console.WriteLine (path);
Console.WriteLine ("Enter a relative path to complete the merge:");
Console.WriteLine ();
while (true)
{
Console.WriteLine ("Merge as:" + Merge Path (Console.ReadLine ()));
Console.WriteLine ("Merge as:" + merge Uri (Console.ReadLine ()));
Console.WriteLine ();
}
}
private static string merge path (string p)
{
Return Path.Combine (Path.getdirectoryname (Path), p);
}
Private static string merge Uri (string p)
{
return new Uri (new Uri (path), p). Absoluteuri;
}
}
Test results:
Great, perfect support ". /"form of the relative path!"
Perfect
The next step is to convert the path to a URI, then flatten the relative path, and then convert back to the path form.
The conversion is only a method of string processing, the modified code is as follows:
Class Program
{
static string path = @ "c:abc123avatar.html";
static void Main (string[] args)
{
Console.WriteLine (path);
Console.WriteLine ("Enter a relative path to complete the merge:");
Console.WriteLine ();
while (true)
{
Console.WriteLine ("Merge as:" + Merge Path (Console.ReadLine ()));
Console.WriteLine ();
}
}
private static string merge path (string p)
{
return new Uri (new Uri ("file:///" + path). Replace ("\", "/")), P.replace ("\", "/")). Absoluteuri.substring (8). Replace ("/", "\");
}
}
Test results:
Conclusion
The result is very satisfactory, but I always think this is a earthwork, the cottage method, who has a more convenient, orthodox point of approach?
Thank you for coming up with the orthodox wording so soon: Path.GetFullPath (Path.combin (@ "C:ac", "... text"));
I have been looking for so long, and toss so long, just to make a cottage to come, really ashamed ah, hehe.