3

In my programs I frequently have file names and/or paths that are configured in my app.config file. This will usually be something like:

<add key="LogFileDirectory" value="C:\Logs" />
<add key="SaveLogFileTo" value="MyLogFile.txt" />

In my actual application code, I'll frequently concatenate these together with code similar to this:

var logFile = ConfigurationManager.AppSettings["LogFileDirectory"]
+ @"\" +
ConfigurationManager.AppSettings["SaveLogFileTo"];

Now, the result of the above code would give a log file path of C:\Logs\MyLogFile.txt, however, if the end-user specifies the log file directory in the configuration file as C:\Logs\ with a trailing backslash, my code results in an actual path of C:\Logs\\MyLogFile.txt with a double backslash between the directory and the file.

In my experience, this works just fine in practice. As a matter of fact, even dropping to a command prompt and executing cd c:\\\\\\windows\\\ works in practice.

My question is, what, if any, are the consequences of having paths like this? I don't want to be using this "feature" in production code if it is something that is undocumented and subject to be broken at some point in the future with a new release of Windows.

John Hodge
  • 1,645
  • 1
  • 13
  • 13
  • 2
    Have you ever heard of the Path class and in particular of the [Path.Combine](https://msdn.microsoft.com/en-us/library/system.io.path.combine(v=vs.110).aspx) method? – Steve May 11 '15 at 21:17
  • 2
    If you are worried about wrong concatenation, try using [`System.IO.Path.Combine`](https://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx). – Styxxy May 11 '15 at 21:17
  • Yes, thank you, I have some familiarity with that method, although don't use it much in practice. I'm more interested in the theory behind multiple backslashes seeming to work without issue and whether or not this is an intentional, documented feature. – John Hodge May 11 '15 at 21:18
  • then why don't you add the double slashes in the filepath in the config file as well as the single trailing `"\"` or use the string literal `@` in front of the logfile like this declare the logfile like this at the class level `private static string logfile { get; set; }` then assign logFile as like this `@logFile = var logFile = ConfigurationManager.AppSettings["LogFileDirectory"]` etc... – MethodMan May 11 '15 at 21:20
  • Perhaps [this](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx) would help – Yuval Itzchakov May 11 '15 at 21:20
  • check this SO Question --> http://stackoverflow.com/questions/22567785/error-with-double-slash-in-windows-path-in-python Also this SO Question --> http://stackoverflow.com/questions/10161177/url-with-multiple-forward-slashes-does-it-break-anything – waltmagic May 11 '15 at 21:22
  • Double backslashes are not permitted in long paths, i.e., paths beginning with \\?\ – Harry Johnston May 12 '15 at 01:55

2 Answers2

3

There are no consequences that I know of, and it's not likely to be broken in future versions, because a lot of people will be doing the same as you.

However, the correct way to combine paths in C# is to use Path.Combine, which will remove any extra backslashes for you:

var logFile = Path.Combine(
    ConfigurationManager.AppSettings["LogFileDirectory"],
    ConfigurationManager.AppSettings["SaveLogFileTo"]);
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • 2
    One thing to be careful of Path.Combine (which bit me today in another answer), from the remarks on the msdn *"If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, path2 is returned."* – Scott Chamberlain May 11 '15 at 21:48
0

At least these kind of consequences there are:

del command cannot delete the file if you provide two backslashes after the drive letter and colon:

C:\>del c:\\foo.txt
The filename, directory name, or volume label syntax is incorrect.

C:\>del c:\\bar\foo.txt
The network path was not found.