GetCurrentDirectory?

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I have built a C# Windows application using a C# library hosted in a DLL.
Both the library and the application need to access some files which at
program installation are placed in the EXE's path; thus, I use
Directory.GetCurrentDirectory() to initialize the default filenames so that
they point to the EXE's path. This happens in the constructor of some
objects (for the DLL) and in the OnLoad event of the EXE's form.

All this works fine in the development workstation (with Visual Studio .NET
and WinXP Pro), and the EXE's path (e.g. ...\MyExe\Bin\Debug or the like) is
retrieved correctly either by the objects in the DLL or in the EXE's OnLoad;
but when I launch the program on a test machine with just Windows ME and
dotnet framework runtime only, the path returned by GetCurrentDirectory is
just the root (C:\)! (I added a test
MessageBox.Show(Directory.GetCurrentDirectory()) instruction to the release
build). Is this anything I'm doing wrong, or any problem with this OS, or
what else?

Thanks to all!
 
Thanks, this works for the EXE... but I'd prefer to avoid referencing the
Windows.Forms namespace for the classes hosted in an external library (a
DLL), which do not belong to any particular application and are pure
"engine" components without any specific user interface... any idea?
 
Hi Dan,

I can not reproduce the problem.

I think the Directory.GetCurrentDirectory will
get the current working directory of the application.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemiodirectoryclassgetcurrentdirectorytopic.asp

while the Application.ExecutablePath will
get the path for the executable file that started the application,
including the executable name.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformsapplicationclassexecutablepathtopic.asp


e.g.
the current directory is c:\
the application is located in the e:\test\test.exe
c:\>e:\test\test.exe
in this case the Directory.GetCurrentDirectory will get c:\
while the Application.ExecutablePath
will get e:\test\test.exe


Did I answer your question?




Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
 
Many thanks, the behaviour of the function now is clear... I can suppose
that the different behaviour in the cited machines happens because VS is
setting the working directory of the debugged application to its output
path, so that this works when debugging or launching it from the IDE but not
when launched from another machine (?). So I'll use something like
Path.GetDirectoryName(Application.ExecutablePath) to get the directory of
the EXE, but this does not apply to a class-library hosted in a DLL: how
could I get the DLL full path in such case?
 
Back
Top