Remove Extra back slash "\" from string file path in c# - c#

How to convert
"String path = #"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\";
into
String path = #"C:\Abc\Omg\Why\Me\".
My approach is to first reverse the string and then remove all the "\" till we get first char, and the reverse it again.
How to do this in C#, is there any method for such operation?

You can just construct path using the Path static class:
string path = Path.GetFullPath(#"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\");
After this operation, variable path will contain the minimal version:
C:\Abc\Omg\Why\Me\

You can use path.TrimEnd('\\'). Have a look at the documentation for String.TrimEnd.
If you want the trailing slash, you can add it back easily.

var path = #"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\";
path = path.TrimEnd('\\') + '\\';
another solution is
var path = #"C:\Abc\Omg\Why\Me\\\\\\\\\\\\\\\\\\\\\";
path = Path.GetFullPath(path);

Related

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
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
To replace bot just call Replace twice:
string destinationFile = System.IO.Path.Combine(appdomain,sourceStreamId)
.Replace(#"\\", #"\")
.Replace("/", #"\");
References: DotNetFiddle Example
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

How to get actual path from string in c#

Hi am facing issue to pass path in File.Copy() method.
Here I have created a string dest. While I am passing it in File.copy(), it is taking "\" double slash. Because of this, I am getting error of illegal character. Please look into it.
string dest = (#"\" + Environment.MachineName +#"\"+ Path.Replace(#"\\",#"\")).Replace(":", "$"); //the value get -"pt-LTP-109\\C$\\Temp\\192.168.0.205\\fileFolder"
dest = dest.Replace("\\\\", #"\") +"\\"+ "filename.txt"; // the value get -"\\pt-LTP-109\\C$\\Temp\\192.168.0.205\\fileFolder\\filename.txt"
dest = ("\"").ToString()+dest+"\""; //the value get- "\"\\pt-LTP-109\\C$\\Temp\\192.168.0.205\\fileFolder\\filename.txt\""
File.Copy(source, dest, true);`
That is a very complicated way of doing something so simple... To convert a normal path into a UNC path you only need to do two things:
Replace : with $ (which you are doing correctly).
Prepend the path with two backslashes and the machine name.
Your code can be shortened to this:
string dest = System.IO.Path.Combine(#"\\" + Environment.MachineName, Path.Replace(":", "$"), "filename.txt");
Try
Path.GetFullPath(dest).Replace(#"\",#"\\");

Convert relative path to another relative path

I have a file test.html on a relative path in c#:
string path1 = "/sites/site/folder/subfolder1/subfolder2/subfolder3/test.html";
Inside the file test.html I have a link to resource:
string path2 = "../../../subfolder4/image.jpg";
I want to calculate complete relative path to this resource against the same relative root as represented in path1 to get the following path3:
string path3 = CalculateRelativePath(path1, path2);
Assert.AreEqual(path3, "/sites/site/folder/subfolder4/image.jpg");
Are there any standard functions to do this? Thank you.
You can use this:
var page = new Uri(new Uri("http://dont-care"), path1);
var path3 = new Uri(page, path2).LocalPath;
I don't think there is a standard function to do this. You could write your own function, splitting both string paths to an array (using "/"), then using the array parts which equal ".." from one, to navigate up the other array.

How Can I split A String from the end to some character I want

How can I split a string from the end to some character I want.
Let me explain in example
"C:\Users\Esat\Desktop\BilimResimler\1620855_759701257391419_1132489417_n.jpg"
and I want to cut this part 1620855_759701257391419_1132489417_n.jpg but I have a lot of image and image names always changing so i can not use substring metod.So how can i do this ?
just to add to the answers - if this refers to a file that physically exists on disk, then why not let fileinfo do the work for you?
var path = #"C:\Users\Esat\Desktop\BilimResimler\1620855_759701257391419_1132489417_n.jpg";
System.IO.FileInfo myImageFile = new System.IO.FileInfo(path);
Console.WriteLine(myImageFile.Name); // gives 1620855_759701257391419_1132489417_n.jpg
You can search for the last "\" character and eliminate everything from it, including him.
OR
From 0 to the index of the length of "C:\Users\Esat\Desktop\BilimResimler\" - 1 (37 - 1 if I counted correctly) keep the string and eliminate everything else.
This should do it
string imageNameAndPath=#"C:\Users\Esat\Desktop\BilimResimler\1620855_759701257391419_1132489417_n.jpg"
imageNameAndPath=imageNameAndPath.Substring(0, imageNameAndPath.LastIndexOf('/'));
string FileName = Path.GetFileName(Path)
You can also get your file name using below code.
var path = #"C:\Users\Esat\Desktop\BilimResimler\1620855_759701257391419_1132489417_n.jpg";
string ImgPath = path.Substring(path.LastIndexOf(#"\") + 1);

Path.Combine() returns unexpected results

I'm trying to create a path using Path.Combine() but I am getting unexpected results.
using System;
using System.IO;
namespace PathCombine_Delete
{
class Program
{
static void Main(string[] args)
{
string destination = "D:\\Directory";
string destination02 = "it";
string path = "L:\\MyFile.pdf";
string sourcefolder = "L:\\";//In other instances, it could be L:\\SomeFolder\AndMayBeAnotherFolder
string replacedDetails = path.Replace(sourcefolder + "\\", "");
string result = Path.Combine(destination, destination02, replacedDetails);
Console.WriteLine(result);
Console.ReadKey();//Keep it on screen
}
}
}
I would expect the result D:\\Directory\it\MyFile.pdf but instead, I get L:\MyFile.pdf
I can't see why? I admit it's late in the evening here, but still, I've used Path.Combine many times, and since .NET 4.0 it allows the string param to be passed. However, it appears to be ignoring the first 2 and only reading the last.
Here is the error
string replacedDetails = path.Replace(sourcefolder + "\\" , "");
You are adding another backslash and nothing is found to be replaced.
Removing the added backslash gives the correct string to search for and replace
string replacedDetails = path.Replace(sourcefolder , "");
however you could avoid all that replace stuff and intermediate variables just with
string result = Path.Combine(destination, destination02, Path.GetFileName(path));
I would recommend using:
string replacedDetails = Path.GetFileName(path);
That will handle removing the source folder from the path without using string replacement, which isn't necessarily reliable if you're getting the path from elsewhere (eventually).
Have you read the documentation? Have you verified what you're passing to Path.Combine()? The documentation says, and I quote:
path1 should be an absolute path (for example, "d:\archives" or "\archives\public").
If path2 or path3 is also an absolute path, the combine operation discards all
previously combined paths and resets to that absolute path.
That should hint at the problem.

Categories