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

将内容过程中比较重要的一些内容片段做个记录,如下内容段是关于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;

			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":
						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
			{
				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

		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

			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" );
			return 1;
		}

		#endregion Error Handling
	}
}       复制代码
                                                                                                                                      


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值