将内容过程中比较重要的一些内容片段做个记录,如下内容段是关于C#获得父进程PID编号的完整源的内容,应该能对大伙有一些用途。

using System;
using System.Diagnostics;namespace RobvanderWoude
{class GetMyPID{static int Main( string[] args ){#region Command Line Parsingbool 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 Parsingtry{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 Handlingpublic 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.00Return or check the parent's (caller's) Process IDUsage:    GETMYPID  [ pid ]  [ /V ]Where:    pid   is the expected PID to check the actual PID against/V    display verbose information on the parent processNotes:    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.EXESET 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 Woudeif ( 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}
}       复制代码

C#获得父进程PID编号的完整源源码相关推荐

  1. VC获取父进程PID

    // tt.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <windows.h>   #include &l ...

  2. linux进程号是几位数,Linux 进程 PID 编号最大为多少

    问题中提到,通过提问者的测试(Ubuntu18.04操作系统下),Python脚本实现的分配进行pid脚本,渠道系统自动分配给进程的最大pid值是131070,这是一个并不特殊的值.(通常可以想到的上 ...

  3. C语言实现pid算法(附完整源码)

    实现pid算法 pid结构体定义 C语言实现pid算法完整源码(定义,实现,main函数测试) pid结构体定义 struct pid {// Controller gainsfloat kP;flo ...

  4. windows 根据父进程pid查找所有子进程id(C++)

    直接上代码: 大家直接调用即可 #include <iostream> #include <Windows.h> #include <tlhelp32.h> #in ...

  5. linux c 获取指定进程 父进程pid

    示例如下: int GetFatherPid(int pid) {char dir[1024]={0};char path[1024] = {0};char buf[1024] = {0};int r ...

  6. python获取进程编号(目的、获取当前进程编号、根据编号杀死指定进程号、获取当前父进程编号)

    1. 获取进程编号的目的         获取进程编号的目的是验证主进程和子进程的关系,可以得知子进程是由那个主进程创建出来的. 获取进程编号的两种操作 获取当前进程编号 获取当前父进程编号 2. 获 ...

  7. 进程控制 父进程子进程 fork pid

    进程:正在运行的程序(分配资源:内存,CPU,IO等) 程序:有限指令的集合(静态:没有执行,存放在外存) 并发:多个任务"同时"执行. (处理器某一时刻最多只运行一个进程) 1. ...

  8. 父进程产生一系列子进程,每个子进程打印自己的PID然后退出,父进程最后打印PID

    #include<stdio.h> #include<stdlib.h> #include <unistd.h> //父进程产生一系列子进程,每个子进程打印自己的P ...

  9. 问题:子进程父进程哪个先执行:【转】关于 fork 和父子进程的理解

    2019独角兽企业重金招聘Python工程师标准>>> 关于 fork 和父子进程的理解   (http://blog.163.com/kingore@126/blog/static ...

最新文章

  1. APICloud:让你的APP边框显示原生的1px
  2. Win32 汇编子过程总结
  3. 你最喜欢的一张美女图片?
  4. 单核工作法17:简化协作(上)
  5. javase期末考试题_java期末考试试题及答案
  6. PowerDesigner新建模板
  7. 微软的创新还是败笔?Windows 8为苹果创造天赐良机
  8. linux go missing git command,安装beego出现go: missing Git command
  9. VMmware安装VMware Tools问题解决
  10. C++ 一定要使用strcpy_s()函数 等来操作方法c_str()返回的指针
  11. linux 添加硬盘
  12. 基于Oracle的SQL优化(社区万众期待 数据库优化扛鼎巨著)
  13. shell初学之PHP
  14. 计算机键盘不能用怎么办,电脑键盘空格键失灵无法使用怎么办|电脑键盘空格键失灵的解决方法...
  15. JNDI(Java Naming and Directory Interface)介绍
  16. 地铁发展黄金期 安防应用受考验
  17. 每日一问。2015.1.8
  18. 在Ubuntu中使用WinUSB创建Windows启动盘
  19. C语言基础(整形变量,实型数据)
  20. XStream的简单使用

热门文章

  1. python语言及其应用-Python语言及其应用.PDF
  2. python创建新文件-python创建和删除文件
  3. 零基础自学python看什么书-学习Python可以看书籍学习吗?老男孩Python入门课程
  4. python语言怎么学-如何从零开始学习Python,python语言编程入门
  5. python爬虫实例-python爬虫实例大全
  6. Linux中和文件相关的操作
  7. LeetCode455 分发饼干(二分法)
  8. 企业应用架构模式学习笔记
  9. Ruby种的特殊变量
  10. maven项目的pom.xml 标签的含义