471,354 Members | 1,509 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

own executalbe path

in c++
int main(int argc,char** argv)

the first argument is the own executable path
how can I can the same info in a c# program?

marcel
Dec 14 '05 #1
7 1338
You can get the path with Windows.Forms.Application.ExecutablePath

http://msdn.microsoft.com/library/de...epathtopic.asp

Dec 14 '05 #2
Unfortunately - that is only usable in Windows Forms apps.
For others (or for consistent use in .NET apps) - try
Environment.GetCommandLineArgs().

And like you noted, the path will be the first argument returned.

--
Adam Clauss

<da***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
You can get the path with Windows.Forms.Application.ExecutablePath

http://msdn.microsoft.com/library/de...epathtopic.asp

Dec 14 '05 #3
Adam Clauss <ca*****@tamu.edu> wrote:
Unfortunately - that is only usable in Windows Forms apps.
For others (or for consistent use in .NET apps) - try
Environment.GetCommandLineArgs().

And like you noted, the path will be the first argument returned.


Actually, Application.ExecutablePath is available in any app as far as
I can see. Try this:

using System;
using System.Windows.Forms;

public class Test
{
public static void Main()
{
Console.WriteLine (Application.ExecutablePath);
}
}

Compile it as a console app, and run it. Works for me :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 14 '05 #4
You could also use Assembly.GetExecutingAssembly to get a reference to
the assembly of the current executable, and then use the Location
property to get the full file name.
You dont need to add a reference to Windows.Forms for this.

Revi

Dec 14 '05 #5
But that also requires a reference to Windows Forms - something a typical
console app does not have :)

--
Adam Clauss

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Adam Clauss <ca*****@tamu.edu> wrote:
Unfortunately - that is only usable in Windows Forms apps.
For others (or for consistent use in .NET apps) - try
Environment.GetCommandLineArgs().

And like you noted, the path will be the first argument returned.


Actually, Application.ExecutablePath is available in any app as far as
I can see. Try this:

using System;
using System.Windows.Forms;

public class Test
{
public static void Main()
{
Console.WriteLine (Application.ExecutablePath);
}
}

Compile it as a console app, and run it. Works for me :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 15 '05 #6
Adam Clauss <ca*****@tamu.edu> wrote:
But that also requires a reference to Windows Forms - something a typical
console app does not have :)


No, but it's something that can easily be added with no problems.

Using Assembly.GetEntryAssembly() is probably a better bet though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 16 '05 #7
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Adam Clauss <ca*****@tamu.edu> wrote:
Using Assembly.GetEntryAssembly() is probably a better bet though.


This is true - I think I ended up going that method a few times in the past.
Actually, only reason I actually thought of Environment.Commandline was I
had just used it in a little test console app I was working with about 10
minutes before I read this post :)

--
Adam Clauss
Dec 16 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by John Roth | last post: by
9 posts views Thread by Bengt dePaulis | last post: by
70 posts views Thread by Michael Hoffman | last post: by
34 posts views Thread by Reinhold Birkenfeld | last post: by
2 posts views Thread by Rob Cowie | last post: by
1 post views Thread by William Stacey [MVP] | last post: by
6 posts views Thread by bukzor | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.