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!
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.
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
I'm pretty sure there's not a way to know this, and that would be why the documentation is "fuzzy".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With