Get location of executing dll

Get location of executing dll

Dale.Bartlett
Collaborator Collaborator
2,480 Views
5 Replies
Message 1 of 6

Get location of executing dll

Dale.Bartlett
Collaborator
Collaborator

I am creating a new post as the one I appended is old and marked solved:

 

The solution accepted here does not work for 2017. It returns the location of the .addin file, not the location of the executing dll which is in a .bundle sub-folder. I have only just picked up that my support help files are not being found, although were previously. Has there been a change for 2017? I have also tried various suggestions with no success. In fact they return the same.

public static string GetDLLFolder()
        {
            string codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            return Path.GetDirectoryName(path);
        }

Thanks, Dale

 




______________
Yes, I'm Satoshi.
0 Likes
Accepted solutions (1)
2,481 Views
5 Replies
Replies (5)
Message 2 of 6

jeremytammik
Autodesk
Autodesk
Accepted solution

Dear Dale,

 

I don't think you need that codebase stuff.

 

I just use:

 

      Assembly exe = Assembly.GetExecutingAssembly();
      string path = exe.Location;

That returns the full pathname of the executing DLL assembly.

 

You can run 

 

Path.GetDirectoryName

 

on that to extract just the directory name.

 

For instance, I use that in the CNC export add-in:

 

https://github.com/jeremytammik/ExportCncFab

 

https://github.com/jeremytammik/ExportCncFab/blob/master/ExportCncFab/App.cs#L61-L65

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 6

Dale.Bartlett
Collaborator
Collaborator

Well I'll be hornswoggled. I thought that would return Revit.exe. Simpler is better. Thanks again. Dale




______________
Yes, I'm Satoshi.
0 Likes
Message 4 of 6

jeremytammik
Autodesk
Autodesk

Dear Dale,

 

Thank you for your appreciation and glad I could help.

 

Cool word, hornswoggled. Never heard it before.

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 6

Dale.Bartlett
Collaborator
Collaborator

And again for completeness... admission of my own stupidity. I was taking Path.GetDirectoryName() of the result, hence stepping back one level. Dummkopf.




______________
Yes, I'm Satoshi.
0 Likes
Message 6 of 6

Sean_Page
Collaborator
Collaborator

One note I'd add here is that if you are using Path.Combine make sure you don't put the "\" before the second path. I just about pulled my hair out trying literally 8-10 solutions for this only to realize it was the Combine that wasn't working correctly.

 

string dir = Path.GetDirectoryName(Assembly.GetExecutingAssemlby().Location)
//Bad:
Path.Combine(dir,@"\path2\file.xml")

//Good:
Path.Combine(dir,@"path2\file.xml"
Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes