Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using / or \\ for folder paths in C#

Tags:

When writing file paths in C#, I found that I can either write something like "C:\" or "C:/" and get the same path. Which one is recommended? I heard somewhere that using a single / was more recommended than using \ (with \ as an escaped sequence).

like image 297
Dominic K Avatar asked Jan 01 '10 05:01

Dominic K


People also ask

What does \\ mean in directory?

means it's the current directory. / is normally used in paths to show the structure of files and folders. \\ is referring to \ (used double because \ is a special character) which is same as / but used differently depending on the OS.

What does \\ mean in Windows path?

They indicate that the path should be passed to the system with minimal modification, which means that you cannot use forward slashes to represent path separators, or a period to represent the current directory, or double dots to represent the parent directory.

How do I set a path to a folder?

Click the Start button and then click Computer, click to open the location of the desired folder, and then right-click to the right of the path in the address bar. Copy Address: Click this option to save the location in a format that is optimized for copying and pasting in Windows Explorer.


1 Answers

Windows supports both path separators, so both will work, at least for local paths (/ won't work for network paths). The thing is that there is no actual benefit of using the working but non standard path separator (/) on Windows, especially because you can use the verbatim string literal:

string path = @"C:\"  //Look ma, no escape 

The only case where I could see a benefit of using the / separator is when you'll work with relative paths only and will use the code in Windows and Linux. Then you can have "../foo/bar/baz" point to the same directory. But even in this case is better to leave the System.IO namespace (Path.DirectorySeparatorChar, Path.Combine) to take care of such issues.

like image 180
Vinko Vrsalovic Avatar answered Oct 02 '22 15:10

Vinko Vrsalovic