3

I need to get the absolute output path of the project's assembly via DTE. I tried doing this using this method, where I would access the OutputPath property, combining it with the assembly name, however this produces the relative path, such as:

..\..\Output\AnyCPU\Debug\MyAssembly.dll

Using Path.GetFullPath is not good for me, because my project might be executing from another location.

I noticed that the $(TargetPath) macro (in Build Events tab in project properties) contains the full path of the assembly. How can I access this value programmatically from the DTE?

Actual question is - how do I get the absolute output path of the project?

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157

1 Answers1

3

I don't know how to programmatically access the "$(TargetPath)", I agree that that could've been the best solution.

However, the approach you mentioned should still be workable,since the OutputPath property is relative to the folder in which the project file resides. (Please let me know if I'm missing some scenario where this is not the case?)

So you can do something similar to this:

      private static string GetProjectExecutable(Project startupProject, Configuration config)
    {
        string projectFolder    = Path.GetDirectoryName(startupProject.FileName);
        string outputPath       = (string)config.Properties.Item("OutputPath").Value;
        string assemblyFileName = (string)startupProject.Properties.Item("AssemblyName").Value + ".exe";
        return Path.Combine(new[] {
                                      projectFolder,
                                      outputPath,
                                      assemblyFileName
                                  });
    }

(the overload of Path.Combine used here is only available in .NET 4.0 but you could always backport it)

Omer Raviv
  • 11,409
  • 5
  • 43
  • 82
  • I'm not sure I follow you - in this case, for MyAssembly, the projectFolder variable will get the value "C:\MyProject\Framework\MyAssembly\", the outputPath will get "..\..\Output\AnyCPU\Debug\", together that would make the path "C:\MyProject\Output\AnyCPU\Debug" as requested, no? – Omer Raviv Mar 30 '11 at 13:50
  • Also, I'm assuming this is a very specific scenario, and not a general thing - in the general case you can't easily pre-determine where a DLL will be loaded from (be it from the output folder, GAC, additional probing dirs specified in .xml config, etc), so I think you'd be better off cross-referencing the list of modules loaded in the process at runtime with the AssemblyName. – Omer Raviv Mar 30 '11 at 13:52
  • The thing is that I don't reference those modules in my runner, rather a need a list of specific assemblies to load them via reflection. – Igal Tabachnik Mar 30 '11 at 14:33
  • Oh, wait, I completely misread your code! You're using the project's `FileName`, not the solution's. This should actually work! – Igal Tabachnik Mar 30 '11 at 14:49