Directory Exists with Path Combine vs String Conca

2020-07-10 08:39发布

So as I am building a Folder/File checking conditional, and a co-worker says it is "better" to use Path.Combine:

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

as opposed to the way I was doing it with

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

Any sound reasoning this is "better?"

5条回答
▲ chillily
2楼-- · 2020-07-10 09:18

It's an interesting question;

You could, of course, write something like:

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

To achieve the result you want.

Using ILSpy, though, lets see why Path.Combine is better.

The overload you are calling is:

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 advantages are obvious; firstly, the function checks for null values and throws the appropriate exception. Then it checks for illegal characters in either 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 thing here are the characters it supports;

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

So it will also support paths where the separator is the wrong way around (like from a urn file://C:/blah, too)

In short, it's better because it gives you validation, a degree of portability (the 3 constants shown above can be defined on a per framework-OS basis), and has support for more than one type of path that you commonly encounter.

查看更多
做个烂人
3楼-- · 2020-07-10 09:18

One really could thing plus the other comments, it is the capability to combine many parts of the directory you want to create.

As an example is this:

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

It supports many characters as it receives an array of string, so it is capable to create the right directory in one single executed line.

Regards,

查看更多
forever°为你锁心
4楼-- · 2020-07-10 09:32

try these two to see the difference.... It can handle URI and standard paths. So 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

查看更多
狗以群分
5楼-- · 2020-07-10 09:38

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

Second, let .Net care about fixing the path. There is a reason we have it. You can be 100% sure that you've done it correctly but if you start combining paths by hand everywhere in your code, there is always a chance to create bugs.

A simple example.

  • User enters a path and you want to create a subfolder named temp there. What will you do?

If no backslash at the end, add one, else do this, otherwise do the other... etc. With Path.Combine() you don't have to do checking. You can concentrate on the actual logic of your application.

查看更多
我只想做你的唯一
6楼-- · 2020-07-10 09:42

Yes, it's more portable in the case that the file-path separator is different to \

查看更多
登录 后发表回答