-1

All the solutions I've found for getting the path of the current file do not work in two situations: when running unit tests from another project in the solution, and when accessing the project from another project with a reference.

When running unit tests, the path generated by these are all relative to the Test project in my solution, and this causes the path I'm building to access some assets to be incorrect:

System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
System.AppDomain.CurrentDomain.BaseDirectory;
System.Environment.CurrentDirectory;
System.IO.Directory.GetCurrentDirectory();
Environment.CurrentDirectory;
Thread.GetDomain().BaseDirectory;
Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
System.IO.Path.GetFullPath(@"..\..\");

They all output the wrong project when running unit tests: C:\Users\Me\source\MySolution\TestProject\bin\Debug

I need it to output the same path to the correct project:
C:\Users\Me\source\MySolution\CoolProject\classes\somefile.cs or C:\Users\Me\source\MySolution\CoolProject\classes or C:\Users\Me\source\MySolution\CoolProject\ or C:\Users\Me\source\MySolution\CoolProject\bin\debug

How to generate a path to the current file or at least the project that the code resides within?

Andrew
  • 18,680
  • 13
  • 103
  • 118
  • 2
    C# is compiled not interpreted. There's no such thing as a 'current file'; there is a thing as a current assembly. It would be hard (impossible?) for compiled code to know where the source code is located at. – Mark Cilia Vincenti Jul 04 '19 at 16:10
  • 1
    Actually, what I would suggest is transverse into the root folder of your solution, then, through the assembly name (which is the same as the project name) you could point to your project's path... some like: C:\Users\Me\Source\MySolution\LOADED_ASSEMBLY_NAME\thefileyouwant.cs – Bruno Jul 04 '19 at 16:14
  • @Andrew what are you trying to achieve with these *.cs files in your tests? – Sten Petrov Jul 04 '19 at 16:18
  • Possible duplicate of [How can I get filename of current working cs file and the name of the current class](https://stackoverflow.com/questions/16295375/how-can-i-get-filename-of-current-working-cs-file-and-the-name-of-the-current-cl) – Christian Gollhardt Jul 04 '19 at 16:19
  • @StenPetrov Just need a base path that I can point to a folder with some assets. Worked fine until ran the code via unit tests ... and within a submodule. – Andrew Jul 04 '19 at 16:21
  • @Andrew if your tests need some file input you can mark the files "Content" or "Embedded Resource" and find them in your current folder or inside the Assembly with GetResourceStream. Traversing the folder structure is not a common thing to do for that need – Sten Petrov Jul 04 '19 at 16:22
  • @StenPetrov Right the tests are not the ones that need the files, the method i call from within the test (in a different project) needs the file, but the path to the file was generated incorrectly when running via unit tests. – Andrew Jul 04 '19 at 16:26

1 Answers1

5

The *.cs is lost during compilation. You can preserve it by having a method with some default parameters and [CallerMemberName] for the method, [CallerFilePath] for the file name and [CallerLineNumber] for the line from which your method is called, then return one of the values received.

public string GetMyFilePath([CallerFilePath] string callerFilePath = null) => return callerFilePath;

invoke it with no parameters and it will return the path to the file you're calling from.... for as long as that's on the same machine where the code was built

You can also get the dll path from Assembly.GetExecutingAssembly().CodeBase. This gives you the full path to the dll with file:/// in front, but not the source file name.

It would be good to elaborate on your objective, there may be a different and better way to do what you're trying to do.

Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
  • This seems to work and be the best option actually, will accept it if nothing else comes in. – Andrew Jul 05 '19 at 12:48