Search code examples
c#stringvisual-studioslash

C# automatically substitutes forward-slash with back-slash in path...why?


I have a textbox where I put the name of a file that contains the forward-slash character '/' When I grab the text from the textbox the '/' is automatically replaced with '\' and it obviously can't find the file, for example:

I write C:\Users\Temp\my/file.txt I get C:\Users\Temp\my\file.txt

So, instead of opening "my/file.txt", it searches for a directory "my" which contains "file.txt"

How can I solve this?


Solution

  • If you're using Windows (which I assume you are, given the paths in your example), then / is an invalid character for a filename:

    Example

    The easiest solution here would be to remove the slash from the filename as this is likely to cause further issues down the line.