how to get the path of an installed DLL of VS .NET Add-in

T

Tee

Hi,

I am creating a VS .NET Add-in. There's an xml config file that will
installed to same folder as the DLL is installed.
The installation path probably is something like C:\Program Files\Company
Name\VSAddin

From the VS .NET Add-in, it needs to locate the xml config file and
read/write data to it.
Anyone know how can I get the folder location by coding?
I tried Application.StartupPath and also
AppDomain.CurrentDomain.BaseDirectory, but none of them just work.
What I get is something like C:\Program Files\Visual Studio 2003\Common\IDE
....

Can anyone help?

Thanks.


Regards,
Tee
 
A

Arne Janning

Hi Tee!

I am creating a VS .NET Add-in. There's an xml config file that will
installed to same folder as the DLL is installed.
The installation path probably is something like C:\Program Files\Company
Name\VSAddin

From the VS .NET Add-in, it needs to locate the xml config file and
read/write data to it.
Anyone know how can I get the folder location by coding?
I tried Application.StartupPath and also
AppDomain.CurrentDomain.BaseDirectory, but none of them just work.
What I get is something like C:\Program Files\Visual Studio
2003\Common\IDE

If you have a reference to your assembly you can use Assembly.Location.
However I don't think it is a good idea to store the config-file in the
application-folder because that would require write-permissions on that
folder. Users normally don't have write-permissions on the
application-folder. Better use the ApplicationData-folder for a specific
user.

Cheers

Arne Janning
 
T

Tee

Hi Arne,

Thanks for your reply.
Can you provide me with a little bit more of information?

What do you mean by I have reference to the assembly ?
My previous question was how the add-in detect it's own DLL location ...
and the Assembly.Location is under what namespace?

Thanks.
Tee
 
A

Arne Janning

Hi Tee!

Thanks for your reply.
Can you provide me with a little bit more of information?

What do you mean by I have reference to the assembly ?
My previous question was how the add-in detect it's own DLL location ...
and the Assembly.Location is under what namespace?

Import the System.Reflection-namespace.

Let's say you have defined a class in your assembly: MyAddinClass

You can get a reference to the assembly (the DLL) in which this class is
defined:

Assembly a = Assembly.GetAssembly(typeof(MyAddinClass));

Now a.Location contains the full-path to your DLL:

Debug.WriteLine(a.Location);

Cheers

Arne Janning
 
T

Tee

Arne,

Thanks for the code, it works.
But while waiting you or someone else to reply, I found that
Assembly.GetExecutingAssembly().Location work the same.

Between this 2 methods, what's the differences ?

and which to go with? any idea?



Thanks.

Tee
 
A

Arne Janning

Hi Tee!

Thanks for the code, it works.
But while waiting you or someone else to reply, I found that
Assembly.GetExecutingAssembly().Location work the same.

Between this 2 methods, what's the differences ?

and which to go with? any idea?

If you for example develop a Windows.Forms-application and have one EXE
(GUI) and one DLL (Business Objects) then using
Assembly.GetExecutingAssembly().Location in your DLL will contain the path
for the EXE-file. I wasn't sure if the
Assembly.GetExecutingAssembly().Location of an add-in-dll would point to the
hosting application or not, this is why I didn't make this proposition. But
if it works as well, even better.

In contrast Assembly.GetAssembly(Type t) can get the assembly for every
known .NET-Type at runtime. E.g. Assembly.GetAssembly(typeof(int)).Location
will give you the path for the mscorlib.dll.

Cheers

Arne Janning
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top