Filepaths on Linux and Windows
-
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
-
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
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.
-
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.
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.txtThis is the text in test1.text
This is the text in test2.textAnd 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.txtThis is the text in test1.text
This is the text in test2.textSo 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