6

I am trying to replace a \ with \\ and it works with everything except the specific variable I need it to work on. Throwing the error Illegal characters in path. probably because it thinks \t is a character, which is tab and is therefor not allowed in a path

the variable is loaded in from a json file using Newtonsoft.Json in to a class

public class WebsiteConfig
{
   public string Name { get; set; }
   public string Directory { get; set; }
}

I have tried var escapedir = Regex.Replace(Directory, @"\\", @"\"); and any possible way I could form var escapedir = Directory.Replace("\", "\\");.

Trying Regex.Replace("C:\test", @"\", @"\\"); (C:\test being the exact same as in Directory) worked perfectly and then inside a foreach I am trying to combine the Directory with a filename.

"Dump" of current code:

var json = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "config.json"));
_config = JsonConvert.DeserializeObject<Config>(json);

foreach(WebsiteConfig website in _config.WebsiteConfigList)
{
  for (int i = 0; i <= 9; i++)
  {
    string dir = website.Directory;
    string escapedir = Regex.Replace(dir, @"\\", @"\\\\");
    var path = Path.Combine(escapedir, "Backedup_" + i.ToString() + ".txt");
  }
}

And config.json:

{
  "WebsiteConfigList": [
    {
      "Name": "test",
      "Directory": "C:\test"
    }
  ]
}

Here's a screenshot to show the exception: Screenshot of code

HackerNCoder
  • 276
  • 1
  • 9
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/204405/discussion-on-question-by-hackerncoder-replace-with-doesnt-work-for-specif). – Samuel Liew Dec 17 '19 at 11:09
  • @SamuelLiew Please reopen. An answer was found and I believe it is on topic. Research was done. Examples were posted. It's reproduceable. – Heki Dec 17 '19 at 11:25
  • This is not off-topic. It has been edited, it is a real problem that can be reproduced and an answer has been found. Please reopen it so that it can be answered and found by others in the future. – HackerNCoder Dec 17 '19 at 11:38
  • 3
    Your JSON file was badly produced. `\t` is a JSON escape sequence. – IS4 Dec 17 '19 at 11:40
  • Yes. The anwser has found a way around that. – HackerNCoder Dec 17 '19 at 11:41
  • @Heki The question has been reopened. Please post your answer – Martin Dec 17 '19 at 14:45
  • json should be like *"Directory": "C:\\test"* for working, otherwise .net compiler regards \ as special command `\t` – Oleg Bondarenko Dec 17 '19 at 15:50
  • @HackerNCoder I got around to posting the answer today. – Heki Dec 18 '19 at 08:37

2 Answers2

2

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.

Heki
  • 926
  • 2
  • 15
  • 34
1

If you can't change the source JSON to be "C:\\test" instead of "C:\test" then detect the tab and replace it with what you want

string escapedir = Regex.Replace(dir, @"\t", @"\\\\t");
robbpriestley
  • 3,050
  • 2
  • 24
  • 36
  • 1
    This would cause problems with something like `C:\normal` and `C:\test\ready`. Obviously you can expand the regex to `@"\\([trn])", @"\\$1"`, but if you forget one you'll be in trouble. – Heki Dec 18 '19 at 14:29