2

I want a function that give me a name of program(like msPaint or notepad) that when i work with notepad, this function return "Notepad" , when i work with msPaint, this function return "msPaint".

Please help me...

a d
  • 552
  • 2
  • 7
  • 17

4 Answers4

3

Using Process.GetProcesses(); method you can get all running application and if you want current active window then make use of GetForegroundWindow() and GetWindowText().

for example click here

Gajendra
  • 1,291
  • 1
  • 19
  • 32
2

You can use the Process class. It has a Modules property that lists all the loaded modules.

To list all processes and all modules to the console:

Process[] processes = Process.GetProcesses();

    foreach(Process process in processes) {
    Console.WriteLine("PID:  " + process.Id);
    Console.WriteLine("Name: " + process.Name);
    Console.WriteLine("Modules:");
    foreach(ProcessModule module in process.Modules) {
        Console.WriteLine(module.FileName);
    }

Or do like this,

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

public static void Main()
{
    int chars = 256;
    StringBuilder buff = new StringBuilder(chars);
    while (true)
    {
        // Obtain the handle of the active window.
        IntPtr handle = GetForegroundWindow();

        // Update the controls.
        if (GetWindowModuleFileName(handle, buff, chars) > 0)
        {
            Console.WriteLine(buff.ToString());
            Console.WriteLine(handle.ToString());
        }
        Thread.Sleep(1000);
    }
}
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

From: How to get a process window class name from c#?

    int pidToSearch = 316;
    //Init a condition indicating that you want to search by process id.
    var condition = new PropertyCondition(AutomationElementIdentifiers.ProcessIdProperty, 
        pidToSearch);
    //Find the automation element matching the criteria
    AutomationElement element = AutomationElement.RootElement.FindFirst(
        TreeScope.Children, condition);

    //get the classname
    var className = element.Current.ClassName;
Community
  • 1
  • 1
0

I am not sure how you are calling these programs. If you running these programs through Process, you can get through ProcessName.

Example:

        Process tp = Process.Start(@"notepad.exe", "temp");

        string s = tp.ProcessName;
KbManu
  • 420
  • 3
  • 7