Start up directory for my dll

R

roto23

I wrote a complied c# class (SpulClass). In that class I use this to
get the directory it resides in..

System.IO.Directory.GetCurrentDirectory();

When I call this this class (dll) from a c# windows application
this....
System.IO.Directory.GetCurrentDirectory(); returns the correct path
(C:\Roto\Work\MyProject)

When I call this this class (dll) from a c# web service this....
System.IO.Directory.GetCurrentDirectory(); returns the incorrect path
(C:\Windows\System32)


the code...
System.IO.Directory.GetCurrentDirectory(); is in the dll, not the web
app. Why is this doing this? and more importantly, what should I put
in my dll such that the correct path is returned wether the dll is
called from a Web service, Desktop app or what ever.


Thanks in advance
-Roto-
 
S

Stoitcho Goutsev \(100\)

Roto,

Current directory i a property of the OS and has nothing to do with the
location where the dll has been loaded from or exe's startup path. I don't
know which one you want to get, but take a look at Assembly class properties
there is a proeprty for the DLL phisical location. For Windows forms
application you can use Application.StartupPath property for getting exe's
startup location. where for web application you can use path mapping
mechanism to get similar information.
 
J

Jon E. Scott

When I call this this class (dll) from a c# web service this....
System.IO.Directory.GetCurrentDirectory(); returns the incorrect path
(C:\Windows\System32)

the code...
System.IO.Directory.GetCurrentDirectory(); is in the dll, not the web
app. Why is this doing this? and more importantly, what should I put
in my dll such that the correct path is returned wether the dll is
called from a Web service, Desktop app or what ever.


Services run from the System32 folder. DLLs are run within the context of
the calling application. In order for your DLL to determine it's own path,
you'll need to use the GetModuleFileName() API:

http://www.pinvoke.net/default.aspx/kernel32/GetModuleFileName.html

I use this in my Delphi DLLs and works great. However, you need to pass in
the HInstance of the DLL module itself in the first parameter, which I've
yet to find in C#. IntPtr.Zero in the above example will return the calling
application path. Maybe someone else can inform us of how to get the
instance handle within a DLL project for this to work.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


| the code...
| System.IO.Directory.GetCurrentDirectory(); is in the dll, not the web
| app. Why is this doing this? and more importantly, what should I put
| in my dll such that the correct path is returned wether the dll is
| called from a Web service, Desktop app or what ever.

First of all a dll does not start-up, some other executable does and then
the dll is loaded either by the framework or by code. In case you want to
know where the dll was loaded from (in case you have a config file in the
same dir for example) you have to use Assembly.CodeBase. You have to use it
from inside the dll though. Usually you go by

Asssembly.GetExecutingAssembly().CodeBase
 
B

Boaz Ben-Porat

Try this (not tested, but should work)

Assembly assm = Assembly.GetExecutingAssembly();
string location = assm.Location;
string assemlyPath = Path.GetDirectoryName(location); // This is the
directory where your DLL reside.

NOTE: If the asembly is in the GAC, this code will return the GAC path
("C:\WINDOWS\assembly").

Hope it helps

Boaz Ben-Porat
Milestone Systems A/S
Denmark
 

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