marceloo1223
marceloo1223

Reputation: 95

How can i replace both Forward(//) and backword(\) slash in a string to forward slash(/)?

i have a string like this: D:\\folder\\folder\\folder/folder/folder since it is mixed up with forward and backword slashes the directory couldnt find file but if i change it like this D:\folder\folder\folder\folder\folder the path is correct .

i have try to do it like this

sourceStreamId=D:\\folder\\folder\\folder/folder/folder
string appdomain = HttpRuntime.AppDomainAppPath;
string destinationFile=System.IO.Path.Combine(appdomain,sourceStreamId).Replace("\\", @"\");

but this resulted in a string like this D:\\folder\\folder\\folder/folder/folder

can anybody sugest a work around for this

i have been here:How to replace the double backslash with a single backslash but that string only have double backword slash i have both forward and backword

Upvotes: 0

Views: 242

Answers (3)

NDJ
NDJ

Reputation: 5194

You could leverage Path to fix it for you:

var ourceStreamId = "D:\\folder\\folder\\folder/folder/folder";

var path = Path.GetFullPath(ourceStreamId);
Console.WriteLine(path);
//output: D:\folder\folder\folder\folder\folder

Upvotes: 0

Amit Soni
Amit Soni

Reputation: 3316

try as below:-

string destinationFile=System.IO.Path.Combine(appdomain,sourceStreamId).Replace(@"\\", @"\");

Eg:-

string path = "C:\Hg\temp/test\\LogFile.txt";
path = path.Replace(@"\\", @"\"); 
string output = path.Replace(@"/", @"\"); 

output >>> C:\Hg\temp\test\LogFile.txt

Upvotes: 1

SᴇM
SᴇM

Reputation: 7213

To replace bot just call Replace twice:

string destinationFile = System.IO.Path.Combine(appdomain,sourceStreamId)
                                       .Replace(@"\\", @"\")
                                       .Replace("/", @"\");

References: DotNetFiddle Example

Upvotes: 0

Related Questions