Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Filepaths on Linux and Windows

Filepaths on Linux and Windows

Scheduled Pinned Locked Moved C#
linuxtutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johan Martensson
    wrote on last edited by
    #1

    Could someone clear something up for me... I'm trying to figure out how to work with filepaths on both Linux and Windows. If I create a string like the one below, it will work on Windows but not on Linux because of the backslashes. string s = Application.StartupPath + @"\config\settings.txt"; Would I get the correct path on both operating systems if I use this: string s = Path.GetFullPath(Application.StartupPath + @"\config\settings.txt"); Or do I have to parse the string manually by using "DirectorySeparatorChar"?

    http://johanmartensson.se - Home of MPEG4Watcher

    L 1 Reply Last reply
    0
    • J Johan Martensson

      Could someone clear something up for me... I'm trying to figure out how to work with filepaths on both Linux and Windows. If I create a string like the one below, it will work on Windows but not on Linux because of the backslashes. string s = Application.StartupPath + @"\config\settings.txt"; Would I get the correct path on both operating systems if I use this: string s = Path.GetFullPath(Application.StartupPath + @"\config\settings.txt"); Or do I have to parse the string manually by using "DirectorySeparatorChar"?

      http://johanmartensson.se - Home of MPEG4Watcher

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I have not done this but I think it is wise to avoid all backslashes, either by using several Path.DirectorySeparatorChar or several Path.Combine() :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Sorry for any delays in replying, I currently don't always get e-mail notifications.


      J 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I have not done this but I think it is wise to avoid all backslashes, either by using several Path.DirectorySeparatorChar or several Path.Combine() :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Sorry for any delays in replying, I currently don't always get e-mail notifications.


        J Offline
        J Offline
        Johan Martensson
        wrote on last edited by
        #3

        I managed to get OpenSuse up and running with Mono so I could run some tests. I ran the code below with "test1.txt" in application folder and "test2.txt" in a folder called "folder1" under application folder

        char sep = Path.DirectorySeparatorChar;

        string path1 = Application.StartupPath + "\\test.txt";
        string path2 = Path.GetFullPath(path1);
        string path3 = Path.Combine(Application.StartupPath, "test.txt");
        string path4 = Application.StartupPath + sep + "folder1" + sep + "test2.txt";

        richTextBox1.Text = path1 + Environment.NewLine + path2;
        richTextBox1.Text += Environment.NewLine + path3 + Environment.NewLine + path4;
        richTextBox1.Text += Environment.NewLine + Environment.NewLine;

        StreamReader sr = new StreamReader(path3);
        string text = sr.ReadToEnd();
        sr.Close();
        richTextBox1.Text += text;

        sr = new StreamReader(path4);
        text = sr.ReadToEnd();
        sr.Close();
        richTextBox1.Text += Environment.NewLine + text;

        On Linux, the result was this:

        /home/mono/Desktop/PathTest/PathTest/bin/Debug\test.txt
        /home/mono/Desktop/PathTest/PathTest/bin/Debug\test.txt
        /home/mono/Desktop/PathTest/PathTest/bin/Debug/test.txt
        /home/mono/Desktop/PathTest/PathTest/bin/Debug/folder1/test2.txt

        This is the text in test1.text
        This is the text in test2.text

        And on windows, the result was this:

        C:\Documents and Settings\Johan\Desktop\PathTest\PathTest\PathTest\bin\Debug\test.txt
        C:\Documents and Settings\Johan\Desktop\PathTest\PathTest\PathTest\bin\Debug\test.txt
        C:\Documents and Settings\Johan\Desktop\PathTest\PathTest\PathTest\bin\Debug\test.txt
        C:\Documents and Settings\Johan\Desktop\PathTest\PathTest\PathTest\bin\Debug\folder1\test2.txt

        This is the text in test1.text
        This is the text in test2.text

        So the conclusion of this, as you pointed out, using Path.DirectorySeparatorChar or several Path.Combine() is the way to go. Using Path.GetFullPath did not correct a windows-formated path as I thought. Thanks for your help.

        http://johanmartensson.se - Home of MPEG4Watcher

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups