示例#1
0
        /// <摘要>
        /// 显示当前进程中的所有应用程序域
        /// </摘要>
        static void ShowAppDomainsInCurrentProcess()
        {
            // GetAppDomainsInCurrentProcess是托管进程类的一个静态方法
            // 此方法用来获得当前进程中的所有应用程序域
            var appDomains = ManagedProcess.GetAppDomainsInCurrentProcess();

            foreach (var appDomain in appDomains)
            {
                Console.WriteLine("应用程序域 Id={0}, 名称={1}",
                                  appDomain.Id, appDomain.FriendlyName);
            }
        }
        /// <summary>
        /// Show AppDomains in Current Process.
        /// </summary>
        static void ShowAppDomainsInCurrentProcess()
        {
            // GetAppDomainsInCurrentProcess is a static method of the class ManagedProcess.
            // This method is used to get all AppDomains in Current Process.
            var appDomains = ManagedProcess.GetAppDomainsInCurrentProcess();

            foreach (var appDomain in appDomains)
            {
                Console.WriteLine("AppDomain Id={0}, Name={1}",
                                  appDomain.Id, appDomain.FriendlyName);
            }
        }
示例#3
0
        /// <摘要>
        /// 显示指定进程中的应用程序域
        /// </摘要>
        /// <param name="pid"> 参数进程ID</param>
        static void ShowAppDomains(int pid)
        {
            if (pid <= 0)
            {
                throw new ArgumentException("请输入有效指令。");
            }

            ManagedProcess process = null;

            try
            {
                // GetManagedProcessByID是托管进程类的一个静态方法
                // 此方法是用来获得一个托管进程类的实例。
                // 如果不存在相应PID的托管进程,将会抛出ArgumentException异常。
                process = ManagedProcess.GetManagedProcessByID(pid);

                foreach (CorAppDomain appDomain in process.AppDomains)
                {
                    Console.WriteLine("应用程序域 Id={0}, 名称={1}",
                                      appDomain.Id,
                                      appDomain.Name);
                }
            }
            catch (ArgumentException _argumentException)
            {
                Console.WriteLine(_argumentException.Message);
            }
            catch (ApplicationException _applicationException)
            {
                Console.WriteLine(_applicationException.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.GetType());
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine("不能获得该进程。"
                                  + " 确保该进程存在,并且是托管进程。");
            }
            finally
            {
                if (process != null)
                {
                    process.Dispose();
                }
            }
        }
        /// <summary>
        /// Show AppDomains in a specified process.
        /// </summary>
        /// <param name="pid"> The ID of the Process.</param>
        static void ShowAppDomains(int pid)
        {
            if (pid <= 0)
            {
                throw new ArgumentException("Please type a valid PID.");
            }

            ManagedProcess process = null;

            try
            {
                // GetManagedProcessByID is a static method of the class ManagedProcess.
                // This method is used to get an instance of ManagedProcessInfo. If there is
                // no managed process with this PID, an ArgumentException will be thrown.
                process = ManagedProcess.GetManagedProcessByID(pid);

                foreach (CorAppDomain appDomain in process.AppDomains)
                {
                    Console.WriteLine("AppDomain Id={0}, Name={1}",
                                      appDomain.Id,
                                      appDomain.Name);
                }
            }
            catch (ArgumentException _argumentException)
            {
                Console.WriteLine(_argumentException.Message);
            }
            catch (ApplicationException _applicationException)
            {
                Console.WriteLine(_applicationException.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.GetType());
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine("Cannot get the process. "
                                  + " Make sure the process exists and it's a managed process");
            }
            finally
            {
                if (process != null)
                {
                    process.Dispose();
                }
            }
        }
示例#5
0
        /// <摘要>
        /// 列举所有的托管进程
        /// </摘要>
        static void ListAllManagedProcesses()
        {
            // GetManagedProcesses是托管进程类 的一个静态方法
            // 此方法用来获得当前机器上所有托管进程的列表
            var processes = ManagedProcess.GetManagedProcesses();

            foreach (var process in processes)
            {
                Console.WriteLine("ID={0}\t名称={1}",
                                  process.ProcessID, process.ProcessName);
                Console.Write("加载运行时: ");
                foreach (var runtime in process.LoadedRuntimes)
                {
                    Console.Write(runtime.GetVersionString() + "\t");
                }
                Console.WriteLine("\n");
            }
        }
        /// <summary>
        /// List all managed processes.
        /// </summary>
        static void ListAllManagedProcesses()
        {
            // GetManagedProcesses is a static method of the class ManagedProcess.
            // This method is used to get a list that contains all managed processes
            // in current machine.
            var processes = ManagedProcess.GetManagedProcesses();

            foreach (var process in processes)
            {
                Console.WriteLine("ID={0}\tName={1}",
                                  process.ProcessID, process.ProcessName);
                Console.Write("Loaded Runtimes: ");
                foreach (var runtime in process.LoadedRuntimes)
                {
                    Console.Write(runtime.GetVersionString() + "\t");
                }
                Console.WriteLine("\n");
            }
        }