Full path with double backslash (C#)
Asked Answered
S

5

11

Is it possible to get a full path with double backslash by using Path.GetFullPath? Something like this:

C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt

instead of this:

C:\Users\Mammamia\Videos\Documents\CFD\geo_msh\cubeOp.txt

Or is there any other method?

Schlessel answered 15/2, 2012 at 13:15 Comment(4)
Maybe string.Replace with @"\" to @"\\"?Lynnlynna
Why do you need it like this? You can replace single \ with double \\ easily anyway.Hukill
@Aamir: Because, later this file will be used by C++.Schlessel
Also, MSBuild needs the double slashes to handle paths with spaces in them properly. I realize this question was asked 3 years ago, but I was looking to see if there was an alternative to doing the string replace and came across it.Gelding
S
23

Do you mean this?

Path.GetFullPath(path).Replace(@"\", @"\\");
Sike answered 15/2, 2012 at 13:18 Comment(3)
Depending on his requirements this could lead to unexpected results if Path.GetFullPath returns an UNC path like @"\\myserver\myshare\some\file.txt".Lynnlynna
I agree @Uwe - not sure why you'd want to do that, but hey - it's an answer! :)Sike
this is result : D:\\\\Programing\\\\DMS\\\\FileTable.Api\\\...Sharpnosed
A
3

C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt is not a valid path, so I'm not sure why you'd want it, but:

Path.GetFullPath(yourPath).Replace("\\", "\\\\");
Azzieb answered 15/2, 2012 at 13:19 Comment(6)
string.Replace takes char too as parameters. .Replace('\', '\\') should be enough.Heteropolar
@Heteropolar '\\' is not a char.Evermore
@BurakKarakuş you're right. I had no idea what I was thinking when I made that comment...Heteropolar
Two slashes is valid.Huddersfield
@DanGifford Yes, but it's not a valid path. With the slashes escaped the path would look lie C:\\\\Users\\\\Mammamia\\\\Videos\\\\Documents\\\\CFD\\\\geo_msh\\\\cubeOp.txtZwart
@Rick.Okelly - Two slashes is valid without the @ literal symbol. That is what I meant. Four slashes presents quite a mess.Huddersfield
T
2

You can just do this:

Path.GetFullPath(@"C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt")

But i'm not sure why, you want to escape the \ ?

If yes, you can do just this :

 Path.GetFullPath(@"C:\Users\Mammamia\Videos\Documents\CFD\geo_msh\cubeOp.txt")
Thorite answered 15/2, 2012 at 13:20 Comment(0)
A
0

I would recommend doing a String.replace(). I recently had to do this in a project for myself. So if you do something similar to:

String input = Path.GetFullPath(x);
input = input.Replace("\\","\\\\");

I am fairly confident that is what you need :)

Documentation: http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx

Abduct answered 15/2, 2012 at 13:21 Comment(0)
H
0

just apply an @ sign before the string:

String k = @"C:\temp\myfile.txt";

Holloweyed answered 23/3, 2022 at 9:52 Comment(1)
This does not answer the original question.Remus

© 2022 - 2024 — McMap. All rights reserved.