在String C#WinForm应用程序中将'\'替换为'/'

kob*_*bra 3 c# string winforms

任何人都可以建议一种方法来用字符串中的斜杠'/'替换反斜杠'\'.基本上字符串是路径.我已经尝试过使用String的Replace()方法,但它不起作用.

谢谢

Mar*_*ell 10

你需要捕获Replace(字符串是不可变的)的结果,并确保你使用字符转义\:

string path = Directory.GetCurrentDirectory();
path = path.Replace("\\", "/");
Run Code Online (Sandbox Code Playgroud)

有关信息; 大多数内置方法都会接受,并假设你在Windows上,\无论如何都会更常见.如果你想要一个uri,那么使用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"
Run Code Online (Sandbox Code Playgroud)