By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,910 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,910 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Jul 6 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
HO*************@YAHOO.COM wrote:
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)
///

--

(O)enone
Jul 6 '07 #2

P: n/a
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

On Jul 6, 4:10 pm, "\(O\)enone" <oen...@nowhere.comwrote:
HONOREDANCES...@YAHOO.COM wrote:
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)
///

--

(O)enone

Jul 9 '07 #3

P: n/a
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

<HO*************@YAHOO.COMa écrit dans le message de news:
11*********************@57g2000hsv.googlegroups.co m...
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

On Jul 6, 4:10 pm, "\(O\)enone" <oen...@nowhere.comwrote:
>HONOREDANCES...@YAHOO.COM wrote:
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)
///

--

(O)enone


Jul 9 '07 #4

P: n/a
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

On Jul 9, 9:22 am, "Patrice" <http://www.chez.com/scribe/wrote:
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

<HONOREDANCES...@YAHOO.COMa écrit dans le message de news:
1183986568.994124.88...@57g2000hsv.googlegroups.co m...
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
On Jul 6, 4:10 pm, "\(O\)enone" <oen...@nowhere.comwrote:
HONOREDANCES...@YAHOO.COM wrote:
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)
///
--
(O)enone- Hide quoted text -

- Show quoted text -

Jul 9 '07 #5

P: n/a
HO*************@YAHOO.COM wrote:
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)enone
Jul 9 '07 #6

P: n/a
HO*************@YAHOO.COM wrote:
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.

--

(O)enone
Jul 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.