0

The project I am working on gets configuration by pointing at a folder, in the past the was done by a relative path (e.g. "../../api/data/"), however this was not suitable in all cirumstances as depending on assembly location the relative path to the above would be different, so another method was devised using System.Reflection.Assembly.GetExecutingAssembly().Location

Ideally we would combine part of the path received from the above with the destination using path.combine() so if the full destination path was "Folder/Core/api/data" and the result from GetExecutingAssembly() was "Folder/Core/Assembly/Assembly.dll", then the plan was to take everything before "/Assembly/" using something like

assemblyPath.Substring(0, assemblyPath.IndexOf($"/Assembly/"))

then combining the result with "../../api/data/" to get the full path.

However I can see now that this will not work if the assembly path is different and does not contain /Assembly/. So I am now wondering what is the best way to dynamically get the full path. I thought about using a switch statement based on the deployment, however that is not very futureproof.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
irl_steve
  • 13
  • 5
  • @TomV: There's a [.net-assembly] tag; when fixing mis-tagged [assembly] questions, it's normally best to change the tag to that instead of removing. – Peter Cordes Dec 21 '22 at 11:33
  • @PeterCordes, I don't know what .net-assembly is. I'm reluctant to blindly add a tag and end up spamming people watching another tag that turns out to be incorrect. Is it fair that anything with (c# + assembly) should always be .net-assembly? – Tom V Dec 21 '22 at 12:06
  • @TomV: An assembly is is basically a library for .NET (the runtime for languages like C#). If you see it asking about "an assembly" (often about paths to one) or code to query stuff about what's in the library, and no sign of it talking about CPU assembly language, then add that tag. I don't use .net either, so I had to search some years ago when I first saw questions talking about an "assembly" that obviously wasn't assembly language. – Peter Cordes Dec 21 '22 at 12:15
  • What kind of application is the app? If its a .net Core app, than this answer show you how to get the root directory: https://stackoverflow.com/a/51293454/686023 – L01NL Dec 21 '22 at 12:25
  • Regular .Net app unfortunately. – irl_steve Dec 21 '22 at 12:32
  • What is a regular .NET app? A console app / WPF / MAUI / Asp.NET? – L01NL Dec 21 '22 at 12:47
  • Just using .Net 6.0 framework – irl_steve Dec 21 '22 at 13:23

0 Answers0