Directory Exists with Path Combine vs String Concatenation

Since I'm building a folder / file check conditionally, and the employee says that it is "better" to use Path.Combine:

string finalPath = Path.Combine(folder, "file.txt"); 

as opposed to how I did it with

 string finalPath = folder + "\\file.txt"; 

Any sound reasoning that this is β€œbetter?”

+6
source share
5 answers

This is an interesting question:

You could, of course, write something like:

 string finalPath = String.Format("{0}\\file.txt", folder); 

To achieve the desired result.

Using ILSpy , it’s better to understand why Path.Combine .

The overload you are calling:

 public static string Combine(string path1, string path2) { if (path1 == null || path2 == null) { throw new ArgumentNullException((path1 == null) ? "path1" : "path2"); } Path.CheckInvalidPathChars(path1, false); Path.CheckInvalidPathChars(path2, false); return Path.CombineNoChecks(path1, path2); } 

The benefits are obvious; firstly, the function checks for null values ​​and throws an appropriate exception. Then it checks for invalid characters in any of the arguments and throws an appropriate exception. Once it is satisfied, it calls Path.CombineNoChecks:

 private static string CombineNoChecks(string path1, string path2) { if (path2.Length == 0) { return path1; } if (path1.Length == 0) { return path2; } if (Path.IsPathRooted(path2)) { return path2; } char c = path1[path1.Length - 1]; if (c != Path.DirectorySeparatorChar && c != Path.AltDirectorySeparatorChar && c != Path.VolumeSeparatorChar) { return path1 + Path.DirectorySeparatorChar + path2; } return path1 + path2; } 

The most interesting here are the characters that it supports;

 Path.DirectorySeparatorChar = "\\" Path.AltDirectorySeparatorChar = "/" Path.VolumeSeparatorChar = ":" 

Thus, it will also support paths in which the separator does not fit (for example, from the file://C:/blah urn)

In short, this is better because it gives you validation, portability (the 3 constants shown above can be defined for each database base) and supports more than one type of path that you usually encounter.

+7
source

try these two to see the difference. It can handle URIs and standard paths. Therefore, always use Path.Combine .

 Console.WriteLine(Path.Combine(@"file:///c:/temp/", "x.xml")); 

Output file:///c:/temp/x.xml

 Console.WriteLine(Path.Combine(@"C:\test", "x.xml")); 

Output C:\test\x.xml

+2
source

Yes, it is more portable when the file path separator is different from \

+1
source

First you can use this notation @"\file.txt instead of "\\file.txt";

Secondly, let .Net take care of fixing the path. We have a reason. You can be 100% sure that you did it right, but if you start combining paths manually in your code, you can always create errors.

A simple example.

  • The user enters the path, and you want to create a subfolder there called temp . What are you going to do?

If there is no backslash at the end, add it, otherwise do it, otherwise do another ... etc. With Path.Combine() you don't have to check. You can focus on the actual logic of your application.

0
source

In fact, there may be other comments, this is an opportunity to combine many parts of the directory that you want to create.

As an example:

 Path.Combine(root, nextFolder, childfolder, file); 

It supports a lot of characters, since it receives an array of strings, so it can create the correct directory in one executed line.

Hello,

0
source

Source: https://habr.com/ru/post/947259/


All Articles