What is the .NET equivalent of java System.getProperty ("user.dir")?

I am trying to get the full path to a file in my unit test, which is located in my project folder. I tried to use

Directory.GetCurrentDirectory();

but it returns the directory in which my tests run. I want the project (or solution) directory not to contain its hard code. Then I can add the last part of the file name. Something like

System.getProperty("user.dir")

in java

+3
source share
2 answers
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

will give you the path to the unit test launched by the DLL itself.

, - Build Action = Content Copy to Output Directory = Copy if newer. , . .

:

  • , <project root>\TestData\Data1.txt.
  • , <project root>\bin\Debug\TestData\Data1.txt.
  • Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TestData\Data1.txt").
+1
string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string folderPath = System.IO.Path.GetDirectoryName(filePath);

, # win-form, WPF asp-net, , .

0

Source: https://habr.com/ru/post/1724402/


All Articles