Replace '\' with '/' in a String C # WinForm Application

Can someone suggest a way to replace the back-slash '\' with a slash '/' in a string. Basically a string is a path. I tried using the Replace () method for the string, but it doesn't work.

thanks

+3
source share
2 answers

You will need to write the result Replace(the lines are immutable) and ensure that character escaping is used for \:

string path = Directory.GetCurrentDirectory();
path = path.Replace("\\", "/");

For information; most of the built-in methods will be accepted either, and if you are on Windows, it \will be more common anyway. If you want to get uri use Uri:

string path = Directory.GetCurrentDirectory();
Uri uri = new Uri(path); // displays as "file:///C:/Users/mgravell/AppData/Local/Temporary Projects/ConsoleApplication1/bin/Debug"
string abs = uri.AbsolutePath; // "C:/Users/mgravell/AppData/Local/Temporary%20Projects/ConsoleApplication1/bin/Debug"
+10

, , , \; , .

, # escape-, @. , , escape- .

, \ , \, @. , :

path = path.Replace(@"\", "/")

:

var path = "C:\\Documents and Settings\\My Documents\\SomeFile.txt";

:

var path = @"C:\Documents and Settings\My Documents\SomeFile.txt";

.

+1

Source: https://habr.com/ru/post/1717417/


All Articles