own executalbe path

  • Thread starter Thread starter daniel.manges
  • Start date Start date
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
 
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 said:
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 :)
 
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
 
But that also requires a reference to Windows Forms - something a typical
console app does not have :)
 
Adam Clauss said:
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 said:
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 :)
 

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

Similar Threads

String vs. string vs. Uri ? 2
SecurityException 2
enum types 4
Trim not working 3
Scroll Bars Visible 1
Hashtable.Item 2
XML 1
Extra spaces after decryption 3

Back
Top