How do I find path to a dll from within the dll?

  • Thread starter HONOREDANCESTOR
  • Start date
H

HONOREDANCESTOR

Suppose I have a dll which might be installed in the directory c:
\MyClass\. I want to be able to find the path of this dll from
within the dll. In other words, I want a function that can return "c:
\MyClass". I cannot use Application.GetExecutablePath from within
the dll, because that gives me the path of the application that is
calling the dll.
This question was also asked on this forum back in 2002, and Microsoft
had no answer to it. Has any progress been made since? It is a
useful feature to have, because I want to distribute my dll with a
database that it writes to, and for it to write to the database, it
needs to know the path to that database, which should be a
subdirectory of the dll.
Thanks,
HA.
 
O

\(O\)enone

Suppose I have a dll which might be installed in the directory c:
\MyClass\. I want to be able to find the path of this dll from
within the dll.

How's this?

\\\
Dim assemblyFilename As String
Dim assemblyPath As String

'Get the filename of the assembly
assemblyFilename = Assembly.GetExecutingAssembly.Location
'Get the path of the assembly
assemblyPath = IO.Path.GetDirectoryName(assemblyFilename)

MsgBox("DLL location is: " & assemblyPath)
///
 
H

HONOREDANCESTOR

Thanks for the trial code. It doesn't really work though. It gives
me the folder of the caller of the dll, not the folder of the dll. I
think this is because the executing assembly is not in the same
location as the actual dll.
Microsoft really seems to have forgotten to give vb.net a function
that will find the location of a dll.
I should add that when I compile my dll, its in the same solution as
my caller program. Nonetheless, its a separate project, and has its
own folder.
Thanks,
HA
 
P

Patrice

What do you do with this information ?

Have you tried to put the suggested code inside your DLL ? Or do you just
tried this code from within your main application ?

--
Patrice

Thanks for the trial code. It doesn't really work though. It gives
me the folder of the caller of the dll, not the folder of the dll. I
think this is because the executing assembly is not in the same
location as the actual dll.
Microsoft really seems to have forgotten to give vb.net a function
that will find the location of a dll.
I should add that when I compile my dll, its in the same solution as
my caller program. Nonetheless, its a separate project, and has its
own folder.
Thanks,
HA
 
H

HONOREDANCESTOR

I did put the suggested code in the dll. It did not give me the path
of the dll.
The reason I want to find the path of the dll is so I can also find
the path of a database installed along with the dll, which the dll
writes to.
-- HA
 
O

\(O\)enone

Thanks for the trial code. It doesn't really work though. It gives
me the folder of the caller of the dll, not the folder of the dll. I
think this is because the executing assembly is not in the same
location as the actual dll.

I think I've figured out what you're describing -- let me see.. Is this what
you're seeing:

1. In your solution is an executable, let's call it MyProject.exe.

2. Also is the DLL whose path you're trying to locate. Let's call it
MyLib.dll. You have a reference to MyLib.dll from MyProject.exe.

3. When you place the code that I posted earlier into MyLib.dll, the path
that is returned is the bin/debug folder from MyProject.exe, and not the
bin/debug path from MyLib.dll.

Am I correct?

If so, I'm afraid what you're seeing is the real and correct answer. When
you compile your solution, all the DLLs required by MyProject.exe that
aren't in the Global Assembly Cache get copied to MyProject.exe's run
directory (its bin/debug dir). This is because the .NET DLL loader will only
look in two places (by default) for DLLs: the application's own directory
and the GAC. As a result, when you add a reference, the compile procedure
copies all the referenced DLLs so that it can find them. If you look in
MyProject.exe's bin/debug directory you'll see that referenced DLLs are all
there.

This is of course unlike the VB6 model, in which each DLL does (or can, at
any rate) execute in its own directory.

So if the question you want to ask is, how can you find at run-time the
original compile location for the DLL (the DLL's own bin/debug directory),
the answer is: you can't. The DLL was copied from there to the .exe's
directory and there's nothing at all to link it back to the original folder.

The only thing that knows anything about the original folder is the IDE. You
could write a macro or add-in that could locate the DLL's bin/debug
directory, but that won't be of any use at all at run-time.

If I've got all this right, why exactly do you want to know the original
compilation path of the DLL? What value does it have? If you describe what
you're trying to do, perhaps we can suggest another way to achieve it.
 
O

\(O\)enone

The reason I want to find the path of the dll is so I can also find
the path of a database installed along with the dll, which the dll
writes to.

I'd suggest you find a different way to identify your db location...

For example, put it into the exe's directory, or add a configuration setting
somewhere which allows you to specify where the database can be found.

Sorry that's probably not much help, but if you read my other post you'll
see why I don't think you'll be able to do this in the way you're trying to
do.
 

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