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.
System.IO.Path.Combine
. – Fluke"\"
or use the string literal@
in front of the logfile like this declare the logfile like this at the class levelprivate static string logfile { get; set; }
then assign logFile as like this@logFile = var logFile = ConfigurationManager.AppSettings["LogFileDirectory"]
etc... – Vineland