简体   繁体   English

从文件路径获取字符串,而不是路径格式

[英]get string from file path not in a path format

I am getting file path of the StreamWriter 我正在获取StreamWriter的文件路径

string fullPath = ((FileStream) (writer.BaseStream)).Name

which gives me: 这给了我:

C:\\P4\\depot\\projects\\_Delegates.generated.cs

Is there chance to get/convert this path to 有机会获得/转换此路径为

C:\P4\depot\projects\_Delegates.generated.cs

without \\\\ but only with \\ in a path. 没有\\\\而只有\\在路径中。

Thanks 谢谢

Try this: 尝试这个:

fullpath = fullpath.Replace(@"\\", @"\");

Basically, replace every double back-slash in the string with a single slash. 基本上,将字符串中的每个双反斜杠替换为一个斜杠。

C:\\P4\\depot\\projects\\_Delegates.generated.cs

--That should be what the actual string literal is, because the extra backslashes are needed as escape sequences. -这应该是实际的字符串文字,因为需要额外的反斜杠作为转义序列。 So in other words, if you were going to define the path yourself by just making a string variable, you would type it in that form... 因此,换句话说,如果您只是通过创建一个字符串变量来自己定义路径,则可以以这种形式键入它...

string fullpath = "C:\\P4\\depot\\projects\\_Delegates.generated.cs"

But, if you were to actually print out the value with Console.WriteLine(fullpath); 但是,如果要使用Console.WriteLine(fullpath);实际打印出该值Console.WriteLine(fullpath); The value you would actually see printed out would be: 您实际看到的输出值为:

C:\\P4\\depot\\projects\\_Delegates.generated.cs

In other words, its basically already in the format you need. 换句话说,它基本上已经在您需要的格式中。

You can use the Regex Replace function from Regular Expression 您可以使用正则表达式中的Regex Replace函数

using System.Text.RegularExpressions; 使用System.Text.RegularExpressions;

private string Inhalt1 = null;
StreamWriter file = new StreamWriter(WindowsPath);

Regex rgx= new Regex(@"\\s");
NewPath= rgx.Replace(file, "\");   // the new Path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM