I don't think there a built-in way to find this out. However I think you could look up the parent process and use that as fairly good heuristic. A quick test shows that the parent process is "explorer" when started from Run (Win+R) or double clicking. It would probably be cmd or powershell any other time except when debugging in VS, then devenv will be the parent process. Obviously, if there are scenarios where other tools will start an instance of the process you may want to give a command line parameter to force a particular behavior.
You code would look something like this:
// Note: Adapted from Hans Passant's answer linked above.
private static string GetParentProcessName()
{
var myId = Process.GetCurrentProcess().Id;
var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", myId);
var search = new ManagementObjectSearcher("root\\CIMV2", query);
var queryObj = search.Get().OfType<ManagementBaseObject>().FirstOrDefault();
if (queryObj == null)
{
return null;
}
var parentId = (uint)queryObj["ParentProcessId"];
var parent = Process.GetProcessById((int)parentId);
return parent.ProcessName;
}
static void Main()
{
/*
Program code here.
*/
if (string.Equals(GetParentProcessName(), "explorer", StringComparison.InvariantCultureIgnoreCase))
{
Console.ReadLine();
}
}