This question already has answers here:
How do I find the parent directory of a path?
(4 answers)
Closed 6 years ago.
I would like to cut last slash and corresponding file or folder given path in string variable (called pathVar)
For example, if pathVar = "c:\temp\a\b\c"
I would like to assign to newPathVar = "c:\temp\a\b"
What is the easier and best way in c# to do that?
Thanks!
You can use Path.GetDirectoryName for that.
Related
This question already has answers here:
Remove dots from the path in .NET
(10 answers)
Closed 1 year ago.
Is there a built in way of removing unnessacary statements like this \hello\.. from a path in C# or do I have to do this using regex replace?
Example:
C:\Users\me\myfolder\..\anotherFolder\image.png to C:\Users\me\anotherFolder\image.png
C:\Users\me\myfolder\..\..\you\f\image.png to C:\Users\you\f\image.png
my solution using regex would be removing this /\\([^\.\\]+)\\\.\./ using a loop regexr.com
You should just be able to write this:
string fixedPath = Path.GetFullPath(#"C:\Users\me\myfolder\..\anotherFolder\image.png"));
Result in fixedPath:
C:\Users\me\anotherFolder\image.png
This question already has answers here:
get path for my .exe [duplicate]
(4 answers)
Closed 5 years ago.
So I'm trying to find a way of accesing the path of the folder to copy files but I can't find a way to do it
string folderpath = "C:\Users\Marc Vila\Downloads\Version 3.5\WindowsForm1\WindowsForm1/Resources";
I don't want to do that because as soon as I move it it gives me an error, anyone knows a more efficient way to do it?
Thanks and regards
Note: I've tried System.IO but it gives me a lot of trouble
You need to get the assembly's path and then build a relative path on top of that.
Take the code from this answer and then use that to combine the paths:
var folderPath = Path.Combine(AssemblyDirectory, "Resources");
This question already has answers here:
Path functions for URL
(5 answers)
Closed 7 years ago.
I need to manipulate paths that are/a/b/c/xxx etc. But Path class is determined that separators must be \. So if I do
var dir = Path.GetDirectoryName("/a/b/c/xxx");
I get "\a\b\c"
I know I can write code to do it myself but I wonder if there is a knob for Path that I haven't found yet
The path functions will only work with paths that are appropriate with your operating system. I suggest you just use a string.Replace.
string.Replace("/", #"\")
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Path.Combine for Urls?
I have a root directory like http://localhost/
I have a file name call sample.jpg
when I use Path.Combine(root, file), I get something like http://localhost\sample.jpg, I am wondering if I can get http://localhost/sample.jpg.
Path.Combine is designed for file system paths, not URLs, so I don't think it will give you what you desire in this case. You can always do the Path.Combine, followed by a String.Replace("\", "/") to correct your URL.
This question already has answers here:
How to implement glob in C#
(4 answers)
Closed 6 years ago.
Given a path c:\someFolder\**\*.exe. How can I get a list of files using this directory path. I know that one could use Directory.GetFiles(directoryPath) but this only works when there are no wildcard characters in directoryPath.
See: How to implement glob in C#