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"
}
]
}