Asp. NET Tutorial: A method of flattening absolute and relative paths

Source: Internet
Author: User
Tags object net readline relative return string static class

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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.