-5

In my project I save xml-files to a folder. The variable "header" contains the name of the file.

string header = "anyString";

The line to save the file is:

xmlDoc.Save($@"D:\00_Coding\00_Repos\MarcSchaetz\MarcSchaetz.STCut\Data\{header}.cutml");

That works good and all, except when the header contains a slash (/) like:

string header = "d/d";

Then I get a DirectoryNotFoundException because Visual Studio couldn't find the path

"D:\00_Coding\00_Repos\MarcSchaetz\MarcSchaetz.STCut\Data\d\d.cutml"

So Visual Studio replaces the slash automatically with a backslash. But why and how can I still save a file with a slash in my variable?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
MarcSchaetz
  • 484
  • 4
  • 11

1 Answers1

1

Visual Studio does not replace the / with \. Check this:

string header = "d/d";
string result = $@"D:\00_Coding\00_Repos\MarcSchaetz\MarcSchaetz.STCut\Data\{header}.cutml";

//Result: D:\\00_Coding\\00_Repos\\MarcSchaetz\\MarcSchaetz.STCut\\Data\\d/d.cutml

As for the use of a / in a path - It can't be used for names of files/directories in the operating system. If you try to create on you will get this: (I tried to insert a /)

enter image description here

So it just makes sense that the code with throw a DirectoryNotFoundException because there really is no directory with that path

Gilad Green
  • 36,708
  • 7
  • 61
  • 95