Search code examples
c#visual-studiodllpluginssolidworks

Relative Paths of File.Open() in a DLL


I'm working on a SolidWorks plugin (DLL). It depends on a couple of files (sqlite DB, sketches etc). To install the plugin, you only have to open the DLL with SolidWorks.

In the DLL are some routines that call File.Open() on a number of resource files that I'll refer to by relative paths. The dir. hierarchy would look like:

Plugin/
    plugin.dll
    Sketches/
        various.sldprt
    otherfile.db
    otherdeps.txt
    ...

Where the root dir. of the plugin can be anywhere on the system. The plugin can be easily loaded by SolidWorks with Open File Dialog. The problem is that once I load the DLL, any calls to File.Open() (eg File.Open("Sketches/various.sldprt")) with relative paths won't work because it'll be relative to the dir. where SLDWORKS.exe (SW install dir.) resides instead of the dir. of the DLL. This would mean I'll have to put all my resource files in the SolidWorks install dir., which I would like to avoid. Is that possible?


Solution

  • What you can do is add your addin folder as a prefix to any local path before calling File.Open(). Addin folder can be obtained as :

    string path = System.IO.Path.GetDirectoryName( 
          System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );