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

时间:2013-05-31 20:08:44

标签: c# filepath streamwriter

我正在获取StreamWriter的文件路径

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

给了我:

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

是否有可能将此路径转换/转换为

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

没有\\但在路径中仅包含\

由于

3 个答案:

答案 0 :(得分:2)

试试这个:

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

基本上,用一个斜杠替换字符串中的每个双反斜杠。

答案 1 :(得分:2)

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

- 这应该是实际的字符串文字,因为需要额外的反斜杠作为转义序列。换句话说,如果你打算通过创建一个字符串变量来自己定义路径,你可以用那种形式输入...

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

但是,如果您要使用Console.WriteLine(fullpath);实际打印出该值,您实际看到的值将是:

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

换句话说,它基本上已经是您需要的格式。

答案 2 :(得分:1)

您可以使用正则表达式的正则表达式替换功能

使用System.Text.RegularExpressions;

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

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