Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to determine which process starts my .Net application?

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?

like image 896
Jakub Šturc Avatar asked Sep 10 '08 06:09

Jakub Šturc


People also ask

How can I tell if a process is running C#?

A much quicker method to check a running process by ID is to use native API OpenProcess(). If return handle is 0, the process doesn't exist. If handle is different than 0, the process is running.

How do I find the process name of a program?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column. Click on any column name to sort.

How do you check if another instance of the application is running?

Count() > 1; This works for any application (any name) and will become true if there is another instance running of the same application.

What is process start in C#?

Start(String)Starts a process resource by specifying the name of a document or application file and associates the resource with a new Process component. public: static System::Diagnostics::Process ^ Start(System::String ^ fileName); C# Copy.


2 Answers

Process this_process = Process.GetCurrentProcess();
int parent_pid = 0;
using (ManagementObject MgmtObj = new ManagementObject("win32_process.handle='" + this_process.Id.ToString() + "'"))
{
    MgmtObj.Get();
    parent_pid = Convert.ToInt32(MgmtObj["ParentProcessId"]);
}
string parent_process_name = Process.GetProcessById(parent_pid).ProcessName;
like image 87
Factor Mystic Avatar answered Oct 23 '22 05:10

Factor Mystic


The CreateToolhelp32Snapshot Function has a Process32First method that will allow you to read a PROCESSENTRY32 Structure. The structure has a property that will get you the information you want:

th32ParentProcessID - The identifier of the process that created this process (its parent process).

This article will help you get started using the ToolHelpSnapshot function:

http://www.codeproject.com/KB/cs/IsApplicationRunning.aspx

like image 21
Espo Avatar answered Oct 23 '22 05:10

Espo