Path of a folder inside my project [duplicate] - c#

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");

Related

How do I do a filePath wich depens on the location of the .exe [duplicate]

This question already has answers here:
get path for my .exe [duplicate]
(4 answers)
Closed 1 year ago.
I am doing a program which reads .txt files and I want to access those without having to change manually the filePath directory. I have it like this: "F:\TR\AppPathFinding\AppPathFinding\bin\Debug" Here I have the .exe file, which starts the program. Secondly, I want to access one folder with different .txt files. It is here: "F:\TR\AppPathFinding\AppPathFinding\bin\Debug\GrillSelection" and the .txt name is: "SetGrill1.txt".
How can I access this .txt without having to change the path manually?
filePath = #"F:\TR\AppPathFinding\AppPathFinding\bin\Debug\GrillSelection\SetGrill1.txt";
This is what I need to change.
Someone can help me please?
As far as I understand you always want to access the same folder that is located at your .exe. Using a relative path.
This is very well explained by Kieren Johnstone.
string filePath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + #"\GrillSelection\SetGrill1.txt";

cut last slash and item for path in c# [duplicate]

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.

is there anyway to make system.io.path work with / [duplicate]

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("/", #"\")

Search folder and get files [duplicate]

This question already has answers here:
C# Searching for files and folders except in certain folders
(4 answers)
Closed 8 years ago.
I am having one folder containing all .jpg files. Now I am having some part of file name. I want all files whose name contains that part from that folder.
thank you in advance...
You can use DirectoryInfo.EnumerateFiles to do this. And just give it the search pattern.

Path combine question [duplicate]

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.

Categories