1

I have a .NET Framework (4.7) solution which is referencing a NuGet package. This NuGet package should read from an html and an xml file. These files are both located within the packages' folder (NuGet package) and they are NOT needed by the solution directly, but only by the NuGet package itself.

How can I refer to these files when running the solution that is consuming the mentioned NuGet package?

I tried with ./ but all this does is point to the root directory of my solution instead.

The actual call that I am trying is: File.ReadAllText(path).

I also tried to set Copy to Output Directory property of the files to Copy always and use Build Action as Content. This is copying these files in the packages/<package name>/Content/*.html for example and somehow I guess I can get this path, such as the solution given here: Getting the path of a nuget package programmatically

But this looks complicated to me for such a seemingly simple task. What is the clean way of doing this?

Edit: Renamed question to specify that setting a resource is not an option in this case as some of the functions I'm consuming in the NuGet package are requesting a file path and not a resource.

magicode118
  • 1,444
  • 2
  • 17
  • 26

1 Answers1

2

If this is a nuget package created by you, under any circumstances at runtime a external file should't be used! someone can alter it, if is needed add it as embedded resource.

Anyway, at runtime I would get the folder where the .dll stays like this:

 var currentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
 var rootFolder = Assembly.GetExecutingAssembly().Location.Split(currentAssemblyName)[0];

Edit - should work for both .NET Framework or dotnet core

My mistake, to get the real path for the DLL you need to search for the Assembly, because ExecutingAssembly is from where the .exe was launched

 var assembly = AppDomain.CurrentDomain
                          .GetAssemblies()
                          .SingleOrDefault(assembly => assembly.GetName().Name == "my.awsome.nuget");
 var location = assembly.Location; 
SilentTremor
  • 4,747
  • 2
  • 21
  • 34
  • you can also put a helper function in the package's assembly, then use `Assembly.GetCurrentAssembly()`. But by this point there's pretty much zero benefit compared to using an embedded resource. – zivkan Oct 10 '19 at 13:38
  • Also a possibly easier way to get the directory with the assembly, rather than using split, is to use `Path.GetDirectoryName(assembly.Location)` – zivkan Oct 10 '19 at 13:40
  • `GetExecutingAssembly`, `GetCurrentAssembly` and `AppDomain.CurrentDomain.GetAssemblies` all returned the solution's Debug folder path for me. Getting a resource is not an option for me as I have functions that take a path as an argument and not a resource. – magicode118 Oct 10 '19 at 15:33
  • yes, there is where the nugget stays, if you publish your application, nugget .dll is under the publish folder, so what is the problem? – SilentTremor Oct 11 '19 at 07:27