C#读取路径,而不在每个子文件夹中放入/



我想把文件路径放在File.Copy(path)

如何放置路径而不每次都添加/

在python中,我可以放置r来读取路径,而不添加/:

open(r"path")

C夏普有类似的东西吗?

是的,您可以使用@运算符:var path = @"c:\APathbier";

这些被称为逐字逐句字符串:https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#regular-和逐字逐句的字符串文字

编辑:

此外,在处理路径时,字符串插值可能会派上用场,简而言之,它是替换字符串中值的一种方法。它与string.format相同,但语法更好imo:

var hello = "Hello";
var world = "world";
var helloworldStringFormat = string.Format("{0} {1}", hello, world);
var helloworldStringInterpolation = $"{hello} {world}";

另请参阅:https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/#string-插值

任何你可以这样做的方法:

var someValue = "bier";
var path3 = $"c:\APath\{someValue}";
var path4 = string.Format(@"c:APath{0}", someValue);

还要检查用于处理路径的Path类,以及可选的Directory类。

https://learn.microsoft.com/en-us/dotnet/api/system.io.path?view=net-5.0https://learn.microsoft.com/en-us/dotnet/api/system.io.directory?view=net-5.0

最新更新