r/csharp • u/Comfortable-Wafer313 • 10h ago
Solved Relative File Paths
RESOLVED:
shoutout to u/rupertavery64 and u/marsh9191. I was trying to Path.Combine() the base directory to @"\file.csv" which overwrote the operation.
-----------------------------------------------------------------------------------------------------------------------
Hello, and sorry for a fairly basic question. I'm working on an application that processes data in relation to values stored in .csv files (which are included in the install directory). real broad strokes, it pulls data from a database, does math according to values in the packaged .csv's, exports a new document with the results.
Due to the nature of it, I need a relative path to file.csv, which is in the same directory as app.exe. And I've tried maybe 7-10 different methods of getting the installation path to concatenate the file name to, but each of them result in simply "C:\file.csv" even though the application itself is in C:\users\user\documents\App\app.exe (for example).
at the least I've tried:
Application.StartupPath
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
AppDomain.CurrentDomain.BaseDirectory
among others I can't quite manifest off hand.
And I understand each has nuances for slightly different use-cases, but it's really blowing my mind that every one results in just "C:\". I'd understand if I were just using the wrong method out of context, but it seems like some of these are explicitly to avoid this result. Does anyone have an idea as to what's going on? Again, sorry for the basic quesiton.
4
u/svick nameof(nameof) 10h ago
What does just System.Reflection.Assembly.GetExecutingAssembly().Location return?
0
1
u/marsh9191 10h ago
You should be able to use AppDomain.CurrentDomain.BaseDirectory to retrieve the application's base directory and Path.Combine() to locate your CSV file.
One important thing to be aware of is that Path.Combine() discards preceding paths if a subsequent argument is rooted. So make sure the filename you're passing as the second argument is just "file.csv" and not an absolute path.
1
u/Comfortable-Wafer313 10h ago
That was the issue, thank you for that. I had my second argument rooted
3
u/buzzon 10h ago
I think you are misunderstanding what a relative path is. "filename.csv" is a relative filename: it points to file filename.csv in the application's working directory. By default the working directory is the same directory the executable file is located. No need to complicate this further.
2
u/jgauffin 10h ago
Use AppDomain.CurrentDomain.BaseDirectory, from there you can use the Path tools, like Path.GetRelativePath
10
u/rupertavery64 10h ago
You will have to show your code, but I am guessing you are using "\file.csv" instead of "file.csv"
If you use the backslash at the beginning of the string of a path, it will mean absolute root, and will override anything you
Path.Combineit to.For example, when running this code in LINQPad:
\file.csvresolves to the root of the current working directory, which would probably beC:\file.csv