注册 登录
  • 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C#获得父进程PID编号的完整源代码

OC/C/C++ 水墨上仙 2082次浏览 已收录 手机上查看

C#获得父进程PID编号的完整源代码

using System;
using System.Diagnostics;
namespace RobvanderWoude
{
	class GetMyPID
	{
		static int Main( string[] args )
		{
			#region Command Line Parsing
			bool doCheck = false;
			bool verbose = false;
			Int64 check = -1;
			// Check command line arguments
			foreach ( string arg in args )
			{
				switch ( arg.ToLower( ) )
				{
					case "/v":
					case "/verbose":
					case "-v":
					case "--verbose":
						if ( verbose )
						{
							return WriteError( "Duplicate command line switch /Verbose" );
						}
						verbose = true;
						break;
					case "?":
					case "/?":
					case "-?":
					case "/h":
					case "-h":
					case "/help":
					case "--help":
						// Display help text without error message
						return WriteError( string.Empty );
					default:
						if ( doCheck )
						{
							return WriteError( "Invalid or duplicate command line argument " + arg.ToUpper( ) );
						}
						if ( !Int64.TryParse( arg, out check ) )
						{
							return WriteError( "Invalid command line argument " + arg.ToUpper( ) );
						}
						if ( check < 1 )
						{
							return WriteError( "Invalid process ID " + arg );
						}
						doCheck = true;
						break;
				}
			}
			if ( args.Length > 2 )
			{
				return WriteError( "Invalid command line argument(s)" );
			}
			#endregion Command Line Parsing
			try
			{
				// Use Jared Barneck's ParentProcess class to retrieve the requested parent process info
				int pid = ParentProcess.ParentProcess.ProcessId;
				if ( verbose )
				{
					Console.WriteLine( "Parent name  : {0}", ParentProcess.ParentProcess.ProcessName );
					Console.WriteLine( "Parent path  : {0}", ParentProcess.ParentProcess.FullPath );
					Console.WriteLine( "Parent PID   : {0}", pid );
					if ( doCheck )
					{
						Console.WriteLine( "Expected PID : {0}", check );
						Console.WriteLine( "Match        : {0}", ( pid == check ? "Yes" : "No" ) );
					}
				}
				else
				{
					Console.WriteLine( pid.ToString( ) );
				}
				if ( doCheck )
				{
					return ( pid == check ? 0 : 2 );
				}
				else
				{
					return pid;
				}
			}
			catch ( Exception e )
			{
				return WriteError( e );
			}
		}
		#region Error Handling
		// Code to display help and optional error message, by Bas van der Woude
		public static int WriteError( Exception e )
		{
			return WriteError( e == null ? null : e.Message );
		}
		public static int WriteError( string errorMessage )
		{
			string exeName = Process.GetCurrentProcess( ).ProcessName;
			/*
			GetMyPID,  Version 1.00
			Return or check the parent's (caller's) Process ID
			Usage:    GETMYPID  [ pid ]  [ /V ]
			Where:    pid   is the expected PID to check the actual PID against
			          /V    display verbose information on the parent process
			Notes:    With an expected PID specified, this program returns 'errorlevel'
			          0 if the actual PID matches the expected value, or 2 if not;
			          otherwise, it returns an 'errorlevel' equal to the PID.
			          So to get a batch file's PID, use the following commands:
			              GETMYPID.EXE
			              SET PID=%ErrorLevel%
			
			Credits:  ParentProcess class by Jared Barneck www.rhyous.com/2010/04/30
			          /how-to-get-the-parent-process-that-launched-a-c-application/
			Written by Rob van der Woude
			http://www.robvanderwoude.com
			*/
			if ( string.IsNullOrEmpty( errorMessage ) == false )
			{
				Console.Error.WriteLine( );
				Console.ForegroundColor = ConsoleColor.Red;
				Console.Error.Write( "ERROR: " );
				Console.ForegroundColor = ConsoleColor.White;
				Console.Error.WriteLine( errorMessage );
				Console.ResetColor( );
			}
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "GetMyPID,  Version 1.00" );
			Console.Error.WriteLine( "Return or check the parent's (caller's) Process ID" );
			Console.Error.WriteLine( );
			Console.Error.Write( "Usage:    " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( "{0}  [ pid ]  [ /v ]", exeName.ToUpper( ) );
			Console.ResetColor( );
			Console.Error.WriteLine( );
			Console.Error.Write( "Where:    " );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "pid" );
			Console.ResetColor( );
			Console.Error.WriteLine( "   is the expected PID to check the actual PID against" );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.Write( "          /V" );
			Console.ResetColor( );
			Console.Error.WriteLine( "    display verbose information on the parent process" );
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "Notes:    With an expected PID specified, this program returns 'errorlevel'" );
			Console.Error.WriteLine( "          0 if the actual PID matches the expected value, or 2 if not;" );
			Console.Error.WriteLine( "          otherwise, it returns an 'errorlevel' equal to the PID." );
			Console.Error.WriteLine( "          So to get a batch file's PID, use the following commands:" );
			Console.Error.WriteLine( );
			Console.ForegroundColor = ConsoleColor.White;
			Console.Error.WriteLine( "              GETMYPID.EXE" );
			Console.Error.WriteLine( "              SET PID=%ErrorLevel%" );
			Console.ResetColor( );
			Console.Error.WriteLine( );
			Console.Error.Write( "Credits:  ParentProcess class by Jared Barneck " );
			Console.ForegroundColor = ConsoleColor.DarkGray;
			Console.Error.WriteLine( "www.rhyous.com/2010/04/30" );
			Console.Error.WriteLine( "          /how-to-get-the-parent-process-that-launched-a-c-application/" );
			Console.ResetColor( );
			Console.Error.WriteLine( );
			Console.Error.WriteLine( "Written by Rob van der Woude" );
			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
			return 1;
		}
		#endregion Error Handling
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#获得父进程PID编号的完整源代码
喜欢 (0)
[开心洋葱]
分享 (0)
关于作者:
水墨上仙
……
加载中……