Replace \ with \\ doesn't work for specific variable

The problem does indeed originate with \t but it happens during deserialisation and not with the Path as you might believe. There have been multiple suggestions to replace the backslash with an escaped backslash, but at that point the damage was already done:

The C:\test had become C: est (whitespace is a tab char).

As per your requirement, altering the input file is not an option, so you have to do your escaping before deserialisation. The simplest way I can think of is:

json = json.Replace(@"\", @"\\");

By the way, while Regex.Replace is quite powerfull, string.Replace is adequate.


It doesn't look like you have large JSON files, but if you do, read a bit here on string.Replace on large files.

Tags:

C#

Backslash