C
C#15mo ago
VerpinZal

Is there a way to determine if a Winforms .exe is run by double clicking on it on Windows explorer?

What I wish to achieve is nothing too fancy. I just want to determine if my winforms exe is run from within windows, or through another .exe. A bool like isLaunchedFromWinExplorer or isLaunchedFromAnotherApplication would suffice. I will show Form1 if the former, show Form2 if the latter. Thanks!
7 Replies
jcotton42
jcotton4215mo ago
I mean, you could check the parent process but if you control the other exe that's launching it, a better way is to just pass a command line argument why do you want to do this anyhow?
VerpinZal
VerpinZalOP15mo ago
Thanks. The parent exe is uncontrolled in this context, unfortunately. Parent app will be an app called „mod organizer 2“. It is possible to add executables in it, and run that exe in a VFS environment with deployed mods for video game modding purposes. My exe runs some checks on the installed mods, and displays a report accordingly. I wish to display the report if my exe is run through mo2. And completely another form if it is run from windows explorer.
JakenVeina
JakenVeina15mo ago
what do you mean "is run from within windows"? yes, your WinForms executable is run from "within windows" that's how WinForms works
jcotton42
jcotton4215mo ago
I think they meant "from Windows explorer" but really you should be checking for mod organizer 2 or see if mod organizer 2 allows command line arguments to be passed to what it's running
JakenVeina
JakenVeina15mo ago
okay, so we're asking if a program can tell how it was launched?
VerpinZal
VerpinZalOP15mo ago
To answer my own question: found out how to do exactly what I want to accomplish.
var processId = Process.GetCurrentProcess().Id;
var queryFromId = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", processId);
var searchById = new ManagementObjectSearcher("root\\CIMV2", queryFromId);
var idResults = searchById.Get().GetEnumerator();
idResults.MoveNext();
var queryObject = idResults.Current;
var parentId = (uint)queryObject["ParentProcessId"];
var parent = Process.GetProcessById((int)parentId);
var processId = Process.GetCurrentProcess().Id;
var queryFromId = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", processId);
var searchById = new ManagementObjectSearcher("root\\CIMV2", queryFromId);
var idResults = searchById.Get().GetEnumerator();
idResults.MoveNext();
var queryObject = idResults.Current;
var parentId = (uint)queryObject["ParentProcessId"];
var parent = Process.GetProcessById((int)parentId);
after that,
parent.processName
parent.processName
yields the desired result, either as "explorer", or "ModOrganizer". also for further future reference, including the necessary assembly instruction at the top of the code somehow doesn't work, one needs to right click on the project dependencies tree, click on "Add project reference", and then select System.Management from the resulting window.
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server