none
How to use Path.Combine in Unix / FTP path format? RRS feed

  • Question

  • Hi all,

    Short question. See this:
    Console.WriteLine(Path.Combine("/abc", "def.txt"));
    Returns:
    "/abc\def.txt"
    Of course, I expected "/abc/def.txt". What's wrong I am?

    --
    Best regards,
    Diviner.
    Tuesday, September 2, 2008 3:12 AM

Answers

  • Forward slashes are a Unix convention, Windows likes backslashes.  Try:

      string path = Path.Combine(...);
      path = path.Replace("\\", "/");

    You may run into some other problems, Windows has a different set of allowed path characters than Unix.  Unix allows * for example.  Care to guess how many files got lost when the hapless users typed "rm *"?

    Hans Passant.
    • Proposed as answer by jack 321 Thursday, September 4, 2008 8:00 AM
    • Marked as answer by jack 321 Friday, September 5, 2008 3:11 AM
    Tuesday, September 2, 2008 4:44 AM
    Moderator
  • Original I expected this method can work like this:
    Console.WriteLine(Path.Combine("/abc", "def.txt"))    -->   "/abc/def.txt"
    Console.WriteLine(Path.Combine("c:\abc", "def.txt")) -->   "c:\abc\def.txt"

    I even imagine that it can give me this service:
    Console.WriteLine(Path.Combine("http://www.abc.com", "index.html"))
    -->   "http://www.abc.com/index.html"

    Ha ha....

    Actually I am using this method to compose a FTP link now. The filename ("def.txt") is case by case while the path ("/abc") is stored as a preference. I expected if I change the link style to normal file style instead of ftp link some day, I don't need to modify the coding too much but only change the path from "/abc" to "c:\abc" (for example). So I don't want to compose the whole link myself. So I try to find a more generic function to do this, I found Path.Combine( ).

    So if it still want me to determine the delimiter everytime, I think that I write such a method myself is better.

    Diviner.
    • Proposed as answer by jack 321 Thursday, September 4, 2008 7:59 AM
    • Marked as answer by jack 321 Friday, September 5, 2008 3:11 AM
    Wednesday, September 3, 2008 2:08 AM
  • If you are working with URLs

    Try

    Page.ResolveUrl

    Viral.
    • Marked as answer by jack 321 Friday, September 5, 2008 3:11 AM
    Wednesday, September 3, 2008 4:29 AM

All replies

  • Does this work for you?

    Console.Writeline(Path.Combine("/abc/", "def.txt"));

    (Please note the extra slash in abc)

    Viral.

    Tuesday, September 2, 2008 4:15 AM

  • Viral,

    If the trailing delimiter has to maintain by myself, the method is not come too meaningful. I would rather use "+" to concatanate it up at all.

    Thank you very much.

    --
    Best regards,
    Diviner.
    Tuesday, September 2, 2008 4:27 AM
  • Forward slashes are a Unix convention, Windows likes backslashes.  Try:

      string path = Path.Combine(...);
      path = path.Replace("\\", "/");

    You may run into some other problems, Windows has a different set of allowed path characters than Unix.  Unix allows * for example.  Care to guess how many files got lost when the hapless users typed "rm *"?

    Hans Passant.
    • Proposed as answer by jack 321 Thursday, September 4, 2008 8:00 AM
    • Marked as answer by jack 321 Friday, September 5, 2008 3:11 AM
    Tuesday, September 2, 2008 4:44 AM
    Moderator
  •  
    Let me know what exactly you are trying to achieve. Becuase many version of UNIX are ok with BackSlashes too!

    Viral.
    • Edited by Viral Jain Tuesday, September 2, 2008 5:14 AM Wrong reference
    Tuesday, September 2, 2008 5:12 AM
  • Original I expected this method can work like this:
    Console.WriteLine(Path.Combine("/abc", "def.txt"))    -->   "/abc/def.txt"
    Console.WriteLine(Path.Combine("c:\abc", "def.txt")) -->   "c:\abc\def.txt"

    I even imagine that it can give me this service:
    Console.WriteLine(Path.Combine("http://www.abc.com", "index.html"))
    -->   "http://www.abc.com/index.html"

    Ha ha....

    Actually I am using this method to compose a FTP link now. The filename ("def.txt") is case by case while the path ("/abc") is stored as a preference. I expected if I change the link style to normal file style instead of ftp link some day, I don't need to modify the coding too much but only change the path from "/abc" to "c:\abc" (for example). So I don't want to compose the whole link myself. So I try to find a more generic function to do this, I found Path.Combine( ).

    So if it still want me to determine the delimiter everytime, I think that I write such a method myself is better.

    Diviner.
    • Proposed as answer by jack 321 Thursday, September 4, 2008 7:59 AM
    • Marked as answer by jack 321 Friday, September 5, 2008 3:11 AM
    Wednesday, September 3, 2008 2:08 AM
  • If you are working with URLs

    Try

    Page.ResolveUrl

    Viral.
    • Marked as answer by jack 321 Friday, September 5, 2008 3:11 AM
    Wednesday, September 3, 2008 4:29 AM
  • Thank you Viral. Your information is helpful further.

    Diviner.
    • Edited by Diviner Chan Friday, September 5, 2008 1:46 AM Wrong wording.
    Friday, September 5, 2008 1:45 AM