GetProcessesByName() public static method

Creates an array of Process components that are associated with process resources on a remote computer. These process resources share the specified process name.
public static GetProcessesByName ( string processName, string machineName ) : System.Diagnostics.Process[]
processName string
machineName string
return System.Diagnostics.Process[]
Esempio n. 1
0
 /// <summary>
 /// Kills all processes matching the specified name.
 /// </summary>
 public static void KillProcess(string processName)
 {
     foreach (var process in Process.GetProcessesByName(processName))
     {
         KillProcess(process);
     }
 }
        /// <summary>
        /// Tries to find running VS instance of the specified version. If not, new instance will be launched.
        /// </summary>
        /// <param name="devenvPath">The full path to the devenv.exe.</param>
        /// <returns>DTE instance or null if not found.</returns>
        private static DTE GetDTE(string devenvPath)
        {
            Process[] processes     = Process.GetProcessesByName("devenv");
            Process   devenvProcess = null;

            foreach (Process process in processes)
            {
                try
                {
                    if (process.MainModule.FileName.Equals(devenvPath, StringComparison.InvariantCultureIgnoreCase))
                    {
                        devenvProcess = process;
                        break;
                    }
                }
                catch {}
            }

            if (devenvProcess != null)
            {
                return(GetDTEFromInstance(devenvProcess.Id, 120));
            }

            return(CreateDTEInstance(devenvPath));
        }
Esempio n. 3
0
        /// <summary>
        /// Creates an instance of ReloadedProcess from a supplied process name.
        /// </summary>
        /// <param name="processName">The process name to find obtain Reloaded process from.</param>
        public static ReloadedProcess GetProcessByName(string processName)
        {
            try
            {
                // Create new ReloadedProcess
                ReloadedProcess reloadedProcess = new ReloadedProcess();

                // Get Process by Name
                var process = SystemProcess.GetProcessesByName(processName)[0];

                // Set Process ID
                reloadedProcess.ProcessId = (IntPtr)process.Id;

                // Get Process Handle
                reloadedProcess.ProcessHandle = Native.Native.OpenProcess(Native.Native.PROCESS_ALL_ACCESS, false, (int)reloadedProcess.ProcessId);

                // Set thread id and handle to be that of first thread.
                reloadedProcess.ThreadId = (IntPtr)process.Threads[0].Id;

                // Set thread handle to be that of the first thread.
                reloadedProcess.ThreadHandle = OpenThread(THREAD_ALL_ACCESS, false, (int)reloadedProcess.ThreadId);

                // Retrun Reloaded Process
                return(reloadedProcess);
            }
            catch
            {
                return(null);
            }
        }
 private static void KillMsBuild()
 {
     Process[] msBuildProcesses = Process.GetProcessesByName("MSBuild");
     foreach (Process msBuildProcess in msBuildProcesses)
     {
         msBuildProcess.KillProcessTreeAndWait(TimeSpan.FromSeconds(30));
     }
 }
        public void TestProcss()
        {
            // Specify a process name that is not running
            Run(testProcess, IterationStarting);

            var procs = Proc.GetProcessesByName(testProcess);

            foreach (var p in procs)
            {
                p.Close();
            }

            Assert.True(madeProcess);
            Assert.AreEqual(0, procs.Length);
        }
        private async Task DetectGame()
        {
            try
            {
                _process = SysProcess.GetProcessesByName("GTA5").First();
                if (_process != null)
                {
                    _sharp = new ProcessSharp(_process, Process.NET.Memory.MemoryType.Remote);
                    await SetUIAsync(true);

                    logger.Info("已偵測到 GTA5.exe 。");
                }
            }
            catch (Exception ex)
            {
                logger.Fatal($"DetectGame Failed:{ex.Message}");
            }
        }
Esempio n. 7
0
        private static DTE?GetDTE()
        {
            //TODO: Find a better way to do this. We cannot be sure that it is the actual foreground window :/
            var window  = NativeMethods.GetForegroundWindow();
            var process = Process.GetProcessesByName("devenv").FirstOrDefault(x => x.MainWindowHandle == window);

            if (process == null)
            {
                //Try by just getting the process:
                process = Process.GetProcessesByName("devenv").First();
                if (process == null)
                {
                    return(null);
                }
            }
            var devEnv  = process.MainModule.FileName;
            var version = versionExpr.Match(devEnv).Groups["version"];

            if (!version.Success)
            {
                var ini = Path.ChangeExtension(devEnv, "isolation.ini");
                if (!File.Exists(ini))
                {
                    throw new NotSupportedException("Could not determine Visual Studio version from running process from " + devEnv);
                }

                if (!Version.TryParse(File
                                      .ReadAllLines(ini)
                                      .Where(line => line.StartsWith("InstallationVersion=", StringComparison.Ordinal))
                                      .FirstOrDefault()?
                                      .Substring(20), out var v))
                {
                    throw new NotSupportedException("Could not determine the version of Visual Studio from devenv.isolation.ini at " + ini);
                }

                return(GetComObject <DTE>(string.Format("!{0}.{1}.0:{2}",
                                                        "VisualStudio.DTE", v.Major, process.Id), TimeSpan.FromSeconds(2)));
            }

            return(GetComObject <DTE>(string.Format("!{0}.{1}:{2}",
                                                    "VisualStudio.DTE", version.Value, process.Id), TimeSpan.FromSeconds(2)));
        }
Esempio n. 8
0
        private ProcessInfo GetProcessInfo()
        {
            Proc[] procs = new Proc[0];

            try
            {
                if (Pid.HasValue)
                {
                    procs = new Proc[] { Proc.GetProcessById(Pid.Value) }
                }
                ;
                else
                {
                    procs = Proc.GetProcessesByName(ProcessName);
                }

                var p = procs.FirstOrDefault();

                if (p != null)
                {
                    return(ProcessInfo.Instance.Snapshot(p));
                }
            }
            catch
            {
            }
            finally
            {
                foreach (var p in procs)
                {
                    p.Close();
                }
            }

            return(null);
        }
Esempio n. 9
0
        static IntPtr GetBase(string pname)
        {
            SysProcess handle = SysProcess.GetProcessesByName(pname)[0];

            return(handle.MainModule.BaseAddress);
        }