Wednesday 22 August 2018

C#: Get the full path of running processes

System.Diagnostics namespace provides 'Process.GetProcesses()' method to get all the processes running currently. Once you got the current running process reference, you can use ‘process.MainModule.FileName’ to get the file name of the running process.

Program.cs
using System;
using System.Diagnostics;

class Program
{
    public static void printAllprocesses()
    {
        Process[] processlist = Process.GetProcesses();

        foreach (Process process in processlist)
        {
            try
            {
                String fileName = process.MainModule.FileName;
                String processName = process.ProcessName;

                Console.WriteLine("processName : {0},  fileName : {1}", processName, fileName);
            }catch(Exception e)
            {
                /* You will get access denied exception for system processes, We are skiping the system processes here */
            }
           
        }
    }

    static void Main()
    {
        printAllprocesses();
    }

}

Output
processName : iexplore,  fileName : C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
processName : MSBuild,  fileName : C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe
processName : UpdaterUI,  fileName : C:\Program Files\McAfee\Agent\x86\UpdaterUI.exe
processName : firefox,  fileName : C:\Program Files (x86)\Mozilla Firefox\firefox.exe
processName : devenv,  fileName : C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe
processName : SCNotification,  fileName : C:\Windows\CCM\SCNotification.exe
processName : VsHub,  fileName : C:\Program Files (x86)\Common Files\Microsoft Shared\VsHub\1.0.0.0\vshub.exe
processName : HelloWorld,  fileName : C:\Users\I335077\Documents\Visual Studio 2015\Projects\HelloWorld\HelloWorld\bin\Debug\HelloWorld.exe
processName : javaw,  fileName : C:\Program Files (x86)\SAP\FrontEnd\SAP Document Center\SAPJVM\bin\javaw.exe
processName : mctray,  fileName : C:\Program Files\McAfee\Agent\x86\McTray.exe
processName : Microsoft.VsHub.Server.HttpHost,  fileName : C:\Program Files (x86)\Common Files\Microsoft Shared\VsHub\1.0.0.0\Microsoft.VsHub.Server.HttpHost.exe
processName : SkypeHost,  fileName : C:\Program Files\WindowsApps\Microsoft.Messaging_2.15.20002.0_x86__8wekyb3d8bbwe\SkypeHost.exe
processName : lync,  fileName : C:\Program Files\Microsoft Office 15\root\office15\lync.exe
processName : ucmapi,  fileName : C:\Program Files\Microsoft Office 15\Root\Office15\UcMapi.exe
processName : winword,  fileName : C:\Program Files\Microsoft Office 15\Root\Office15\WINWORD.EXE
processName : DCSHelper,  fileName : C:\ProgramData\DatacardService\DCSHelper.exe
processName : cmd,  fileName : C:\Windows\SysWOW64\cmd.exe
processName : lynchtmlconv,  fileName : C:\Program Files\Microsoft Office 15\Root\Office15\lynchtmlconv.exe
processName : sbus,  fileName : C:\Program Files (x86)\SAP\FrontEnd\SecureLogin\bin\sbus.exe
processName : iexplore,  fileName : C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE
processName : outlook,  fileName : C:\Program Files\Microsoft Office 15\root\office15\OUTLOOK.EXE
processName : plugin-container,  fileName : C:\Program Files (x86)\Mozilla Firefox\plugin-container.exe
processName : notepad++,  fileName : C:\Program Files (x86)\Notepad++\notepad++.exe
processName : HelloWorld.vshost,  fileName : C:\Users\I335077\Documents\Visual Studio 2015\Projects\HelloWorld\HelloWorld\bin\Debug\HelloWorld.vshost.exe
processName : iexplore,  fileName : C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE


Previous                                                 Next                                                 Home

No comments:

Post a Comment