Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.IO.Path or equivalent use with Unix paths

Tags:

c#

.net

path

unix

Is it possible to either use the System.IO.Path class, or some similar object to format a unix style path, providing similar functionality to the PATH class? For example, I can do:

Console.WriteLine(Path.Combine("c:\\", "windows"));

Which shows:

"C:\\windows"

But is I try a similar thing with forward slashes (/) it just reverses them for me.

Console.WriteLine(Path.Combine("/server", "mydir"));

Which shows:

"/server\\mydir"
like image 563
Paul Michaels Avatar asked May 06 '10 09:05

Paul Michaels


People also ask

What is System IO path?

A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform.

What is the directory separator in Linux?

The directory separator in Linux is the forward slash / . So you can think of directories as ending in / , and often this is understood.

What is rooted path?

A rooted path is file path that is fixed to a specific drive or UNC path; it contrasts with a path that is relative to the current drive or working directory. For example, on Windows systems, a rooted path begins with a backslash (for example, "\Documents") or a drive letter and colon (for example, "C:Documents").

What is path combine?

Combines two strings into a path. Combine(String, String, String) Combines three strings into a path. Combine(String, String, String, String) Combines four strings into a path.


1 Answers

You've got bigger problems, Unix accepts characters in a file name than Windows does not allow. This code will bomb with ArgumentException, "Illegal characters in path":

  string path = Path.Combine("/server", "accts|payable");

You can't reliably use Path.Combine() for Unix paths.

like image 64
Hans Passant Avatar answered Oct 10 '22 16:10

Hans Passant