简体   繁体   中英

get string from file path not in a path format

I am getting file path of the 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); 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

using System.Text.RegularExpressions;

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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