<p>I am developing console application in .Net and I want to change a behavior a little based on information that application was started from cmd.exe or from explorer.exe. Is it possible?</p>

[{'answer_id': 53508, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 2, 'selected': False, 'text': '<p>The <a href="http://msdn.microsoft.com/en-us/library/ms682489(VS.85).aspx" rel="nofollow noreferrer">CreateToolhelp32Snapshot Function</a> has a Process32First method that will allow you to read a <a href="http://msdn.microsoft.com/en-us/library/ms684839(VS.85).aspx" rel="nofollow noreferrer">PROCESSENTRY32 Structure</a>. The structure has a property that will get you the information you want:</p>\n\n<blockquote>\n  <p><strong>th32ParentProcessID</strong> - The identifier\n  of the process that created this\n  process (its parent process).</p>\n</blockquote>\n\n<p>This article will help you get started using the ToolHelpSnapshot function:</p>\n\n<p><a href="http://www.codeproject.com/KB/cs/IsApplicationRunning.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/cs/IsApplicationRunning.aspx</a></p>\n'}, {'answer_id': 53526, 'author': 'Factor Mystic', 'author_id': 1569, 'author_profile': 'https://Stackoverflow.com/users/1569', 'pm_score': 4, 'selected': True, 'text': '<pre><code>Process this_process = Process.GetCurrentProcess();\nint parent_pid = 0;\nusing (ManagementObject MgmtObj = new ManagementObject("win32_process.handle=\'" + this_process.Id.ToString() + "\'"))\n{\n    MgmtObj.Get();\n    parent_pid = Convert.ToInt32(MgmtObj["ParentProcessId"]);\n}\nstring parent_process_name = Process.GetProcessById(parent_pid).ProcessName;\n</code></pre>\n'}, {'answer_id': 53565, 'author': 'Adam Mitz', 'author_id': 2574, 'author_profile': 'https://Stackoverflow.com/users/2574', 'pm_score': 2, 'selected': False, 'text': '<p>One issue with the ToolHelp/ManagementObject approaches is that the parent process could already have exited.</p>\n\n<p>The GetStartupInfo Win32 function (use PInvoke if there\'s no .NET equivalent) fills in a structure that includes the window title.  For a Win32 console application "app.exe", this title string is "app" when started from cmd and "c:\\full\\path\\to\\app.exe" when started from explorer (or the VS debugger).</p>\n\n<p>Of course this is a hack (subject to change in other versions, etc.).</p>\n\n<pre><code>#define WIN32_LEAN_AND_MEAN\n#include &lt;windows.h&gt;\nint main()\n{\n  STARTUPINFO si;\n  GetStartupInfo(&amp;si);\n  MessageBox(NULL, si.lpTitle, NULL, MB_OK);\n  return 0;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/53501', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2361/']