What dir executing is done from

M

Mads W.

I want to read an ini file which is located in the same dir as my .exe file.
But how can I do that without having to have the excact same dir for all my
program installations.

Let me try to elaborate:
PC 1 has my C# program lying in C:\Application\Csharp.exe
Here I need to read my ini file in C:\Application\csharpini.ini

PC 2 hs my C# program lying in X:\Programs\Csharpprogram\Csharp.exe
Here I need to read my ini file in
X:\Programs\Csharpprogram\Csharpini.ini

Thx in advance...

Mads W.
 
M

Moty Michaely

Hey Mads,

Try using System.Windows.Forms.Application.StartupPath
Gets the path for the executable file that started the application, not
including the executable name.

-Moty-
 
I

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

hi

Assembly.Location:


Assembly.GetExecutingAssembly().Location


cheers,
 
M

Mads W.

Hi again, thx for your answers.
I couldn't get the Assembly to work.
As my app is a consoleapplication, I didn't try the Windows.Forms thing.

This worked for me:
Environment.CurrentDirectory
The MSDN states that System.Collection myst be used.

Greets...
Mads
 
I

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

hi,

I couldn't get the Assembly to work.

Did you include this line:
using System.Reflection;
As my app is a consoleapplication, I didn't try the Windows.Forms thing.

There is nothing from a win app there.
This worked for me:
Environment.CurrentDirectory

That give you the current directory NO the directory where the application
resides in.
do this:
- open command promt
- cd c:\
- call to your application z:\program\.....\run.exe

I bet you will get an error !

cheers,
 
M

Moty Michaely

Hey,

To use System.Windows.Forms add it as a reference (even if it's a console
application).

Don't forget the minor thing: Environmet variables resides the applications
owner environment variables. (Security issues).

And of course as Ignacio said, CurrentDirectory is the current using
directory. It's not the directory your application runs from.

- Moty -
 
M

Mads W.

Hey again,

You guys were so right!
Ignacio, I tried your suggestion, to call the program from another dir. And
you were right, the dir that is shown is the "called fromr" dir and not the
"executing in" dir.

I tried your suggestion to use System.Reflection - that worked!

Moty, I didn't go with your solution because I had to make a new reference.
I'm not that used to C# yet, but maybe you could answer this for me:

If I add a new reference, do I have to move the DLL files with the program?
Let's say that I add a reference to Outlook Com object. Now I see that
Visual Studio has put a Interop.Outlook.dll in my debug dir. Will I have to
move this DLL around with my program?

Again guys - thanks alot! :)

Greets
Mads
 
M

Moty Michaely

Hey Mads,

It doesn't really matter which method you use for this purpose :). Since
..NET framework includes all dll's it really doesn't affect a thing adding a
reference to System.Windows.Forms in your console application (Not that I
know of..).

About your question: If you have your dll registered it doesn't matter where
the dll exists. The application searches the running directory for it's
referenced dll's and if it's not there it seaerches for the system32
directory.

Hope this helps.

- Moty -
 
I

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

Hi,

good to know that it worked for you !

Moty Michaely said:
Hey Mads,

It doesn't really matter which method you use for this purpose :). Since
.NET framework includes all dll's it really doesn't affect a thing adding
a reference to System.Windows.Forms in your console application (Not that
I know of..).

I think that the problem would be if you try to use a Form ( the only reason
why you would include it ) , a console application does not has a WndProc
method ( the win message handler ) and this is needed to run a win app.

A different case is the System.Web , it does contains the MailMessage class
so if you want to send email you have to include it.
Now I have no idea why MailMessage ( or the Mail namespace ) was include
under the System.Web , does anybody knows why?


Cheers,
 
J

JamesCC

Dear Mads,
Moty, I didn't go with your solution because I had to make a new
reference. I'm not that used to C# yet, but maybe you could answer this
for me:

If I add a new reference, do I have to move the DLL files with the
program?

As far as I know, System.Windows.Forms is part of the .Net framework, and so
as long as the target computer has .Net installed, no other DLLs are
required for that to work. Granted there are different versions of the .Net
framework, and some things work differntly between the versions, but that
should be rare.

Chances are you need the .Net framework for something else you're using, and
so will see no difference. It may use a small amount of extra memory/.exe
code to link in the dll or somesuch, others will know better than I.
Let's say that I add a reference to Outlook Com object. Now I see that
Visual Studio has put a Interop.Outlook.dll in my debug dir. Will I have
to move this DLL around with my program?

For other references, not part of the .Net framework, you will need the
relevant dll's on the target computer - either by shipping your program with
the required dlls, or, if they come from another application (ie Outlook, or
IE for a WebBrowser), you will probably need those other applications
installed. Shipping dlls from other companies may have legal problems,
depending on the company and product.

Hope that helps,

James CC
 

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