How can I get the folder where my grasshopper plug-in/component is located?

How can I get the folder where my grasshopper plug-in/component is located?

I tried

System.AppDomain.CurrentDomain.BaseDirectory

but that gives me location of the Rhino executable.

Thanks,
Thomas

At what point do you want to know this? What I mean is, is your plugin running and are you trying to do this in code?

Yes, the plugin (which is a Grasshopper plugin) is running, and I want to
find the location of the *.gha.

In that case, you can do the following (for any type defined in the Grasshopper GHA file):

MyClass obj = new MyClass(); // any class defined in the GHA file
Type type = obj.GetType();
String location = type.Assembly.Location;

This is .NET framework functionality and works for any class in a .NET DLL or EXE.

In addition to @menno’s very good answer, @ParamDesSing, if you want, you can also use
string path = Assembly.GetExecutingAssembly().Location;

Hi Giulio and Menno,

I’ve tried both versions.
I can get the assembly and type, but location returns an empty string in both cases.

Thanks,
Thomas

If assemblies are loaded via memory, then you cannot use the Location property of the .NET Assembly class.

Instead, you can get the information Grasshopper itself collects about the plugins it loads:

GH_AssemblyInfo info = Grasshopper.Instances.ComponentServer.FindAssembly(<YourPluginId>);
string location = info.Location;

Although note that in this case the location is not actually where your assembly resides, it’s only where the original data for your assembly resides before it was copied into memory and only then loaded.

3 Likes

Yeah, right, I forgot about that option!

Thanks David!

So what should I use for YourPluginID?

I have an Assembly Guid and a Component Guid (Should these be same?), but both return null.

It has to be the assembly ID that you’re using in the class which derives from Grasshopper.Kernel.GH_AssemblyInfo
If you don’t have one, you can also the FindAssemblyByObject method instead and use one of your component IDs.

Though do note that this functionality may not be available during loading. When are you calling this?

1 Like

Thanks, FindAssemblyByObject(myComponent) works!
I’m using it once the component executes it’s code, so after loading.