Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I figure out whether a C# app was launched from the desktop or the cmd shell?

Tags:

c#

How can i find out if a C# App has been started from the desktop or the cmd shell?

The msdn documentation is a bit fuzzy on this part. Perhaps someone can help me out here :)

Thx a lot!

like image 240
MrMarco Avatar asked Sep 08 '09 05:09

MrMarco


3 Answers

This seems to work:

string[] args = System.Environment.GetCommandLineArgs();

if(args[0] == "you exe name"){ ...}

If you double-click it, args[0] contains the full folder.

Note, you need to actually call .GetCommandLineArgs(), the args[] parameter that you have in a typical static void Main(string[] args) has this item removed.

-- Edit

This will only detect if it's run from the same path as the .exe itself. If you run it from a subfolder (foo\hello.exe) it won't work.

like image 151
Noon Silk Avatar answered Sep 26 '22 14:09

Noon Silk


Try getting the parent process:

            var pc = new PerformanceCounter("Process", "Creating Process Id",
            Process.GetCurrentProcess().ProcessName);
        var p = Process.GetProcessById((int)pc.RawValue); 

Not sure if this works when there are multiple instances of the same process, though. In such case it will be better to use this approach: http://www.codeproject.com/KB/threads/ParentPID.aspx

like image 34
skevar7 Avatar answered Sep 25 '22 14:09

skevar7


I'm pretty sure there's not a way to know this, and that would be why the documentation is "fuzzy".

like image 44
David Avatar answered Sep 23 '22 14:09

David