C#Process进程的使用,以及对ProcessInfo中所有的参数详细记录

目录

一、Process

二、ProcessInfo

2.1 FileName

2.2  Arguments

2.3 WorkingDirectory

2.4 CreateNoWindow

2.5 UseShellExecute

2.6 RedirectStandardInput

2.7 RedirectStandardOutput

2.8 RedirectStandardError

2.9 WindowStyle

2.10 Verb

2.11 LoadUserProfile

2.12 UserName

2.13 Password

2.14 Domain

2.15 ErrorDialog

2.16 ErrorDialogParentHandle

2.17 EnvironmentVariables

2.18 StandardInputEncoding

2.19 StandardOutputEncoding

2.20 StandardErrorEncoding

2.21 EnableRaisingEvents

三、获取当前进程的所有信息

四、通过Process进程,打开指定的URL

五、通过Process进程,执行一些exe后缀的文件

5.1 打开高级系统设置

5.2 打开任务管理器

5.3 打开Windows资源管理器

5.4 通过CMD命令行,ping某个IP地址

5.5 通过记事本打开某个txt文件

六、获取输出内容

6.1 输入java -version后获取输出内容

6.2 输入多条指令

6.3  在命令行中输入MYSQL的命令

1. 使用 --connect-expired-password 选项:

 2. 使用交互模式:


一、Process

在C#中,可以使用System.Diagnostics命名空间下的Process类来启动、停止、监视进程。

二、ProcessInfo

2.1 FileName

  • 类型string
  • 说明: 要启动的可执行文件的名称或路径。例如:"notepad.exe",或者可以指定完整路径:"C:\\Windows\\System32\\notepad.exe"
  • 备注: 如果可执行文件在 PATH 环境变量中已定义,则只需指定文件名即可;否则需要提供完整路径。

2.2  Arguments

  • 类型string
  • 说明: 启动可执行文件时要传递的命令行参数。多个参数时,用空格分隔。例如:"-l -h".
  • 示例: 如果要打开 Notepad 并指定文件,可以使用 "notepad.exe" 作为 FileName,以及 "example.txt" 作为 Arguments

2.3 WorkingDirectory

  • 类型string
  • 说明: 设置启动进程的工作目录。如果未指定,默认使用当前目录。
  • 示例WorkingDirectory = @"C:\MyApp"

2.4 CreateNoWindow

  • 类型bool
  • 说明: 如果为 true,则不会为新进程创建控制台窗口。适用于后台运行的进程或服务。
  • 默认值false
  • 备注: 对于需要隐藏的命令行应用或无 UI 的进程,可以将其设置为 true

2.5 UseShellExecute

  • 类型bool
  • 说明: 决定是否使用操作系统的 Shell 执行启动过程。如果为 true,则使用 Shell 启动程序(可以启动任何类型的文件);如果为 false,则直接运行可执行文件。
  • 默认值true
  • 备注: 如果你需要重定向标准输入、输出或错误流,必须将此属性设置为 false

2.6 RedirectStandardInput

  • 类型bool
  • 说明: 决定是否将新进程的标准输入流重定向到父进程,以便父进程可以写入输入数据。
  • 默认值false
  • 备注: 需将 UseShellExecute 设置为 true才能使用此属性。

2.7 RedirectStandardOutput

  • 类型bool
  • 说明: 决定是否将新进程的标准输出重定向到父进程,以便父进程可以读取输出数据。
  • 默认值false
  • 备注: 需将 UseShellExecute 设置为 true才能使用此属性。

2.8 RedirectStandardError

  • 类型bool
  • 说明: 决定是否将新进程的标准错误重定向到父进程,以便父进程可以读取错误信息。
  • 默认值false
  • 备注: 需将 UseShellExecute 设置为 true才能使用此属性。

2.9 WindowStyle

  • 类型ProcessWindowStyle
  • 说明: 指定新进程的窗口显示状态。可以选择:
    • Normal:正常显示窗口。
    • Hidden:隐藏窗口。
    • Minimized:窗口最小化。
    • Maximized:窗口最大化。
  • 默认值Normal
  • 示例process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

 2.10 Verb

  • 类型string
  • 说明: 指定要使用的动作(或谓词)。常见谓词有 "open""print""edit" 等,适用于文件类型关联操作。例如:你可以使用 "print" 来打印文档,或使用 "edit" 来编辑文件。
  • 示例process.StartInfo.Verb = "print";

2.11 LoadUserProfile

  • 类型bool
  • 说明: 指定是否加载用户配置文件。当 UseShellExecute 为 false 时可用。如果为 true,进程将加载用户的配置文件(例如环境变量和注册表配置)。
  • 默认值false
  • 备注: 该属性通常用于启动以不同用户身份运行的进程。

2.12 UserName

  • 类型string
  • 说明: 以特定用户的身份启动进程。需要同时提供 Password
  • 备注UseShellExecute 必须为 false

2.13 Password

  • 类型SecureString
  • 说明: 与 UserName 配合使用,指定运行进程时的用户密码。
  • 备注UseShellExecute 必须为 false

2.14 Domain

  • 类型string
  • 说明: 指定用于启动进程的域(如果用户属于某个域)。
  • 备注: 该属性通常与 UserName 一起使用,指定进程以不同域用户身份运行。

2.15 ErrorDialog

  • 类型bool
  • 说明: 如果为 true,当启动的进程无法启动时,会显示一个错误对话框。
  • 默认值false

2.16 ErrorDialogParentHandle

  • 类型IntPtr
  • 说明: 设置显示错误对话框时的父窗口句柄。通常和 ErrorDialog 一起使用。

2.17 EnvironmentVariables

  • 类型StringDictionary
  • 说明: 提供要传递给新进程的环境变量集合。可以向新进程传递特定的环境变量,而不是使用父进程的环境变量。

2.18 StandardInputEncoding

  • 类型Encoding
  • 说明: 设置标准输入流的编码。
  • 备注RedirectStandardInput 必须设置为 true

2.19 StandardOutputEncoding

  • 类型Encoding
  • 说明: 设置标准输出流的编码。
  • 备注RedirectStandardOutput 必须设置为 true

2.20 StandardErrorEncoding

  • 类型Encoding
  • 说明: 设置标准错误流的编码。
  • 备注RedirectStandardError 必须设置为 true

2.21 EnableRaisingEvents

  • 类型bool
  • 说明: 指定是否在进程退出时向父进程触发 Exited 事件。
  • 默认值false
  • 备注: 设置为 true 后,可以通过订阅进程的 Exited 事件来捕获进程退出。

三、获取当前进程的所有信息

示例

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

// 获取当前进程的所有信息
Process currentProcess = Process.GetCurrentProcess();
// 输出当前进程的详细信息
Console.WriteLine("=== 当前进程信息 ===");
Console.WriteLine($"进程名: {currentProcess.ProcessName}");
Console.WriteLine($"进程ID: {currentProcess.Id}");
Console.WriteLine($"进程句柄: {currentProcess.Handle}");
Console.WriteLine($"启动时间: {currentProcess.StartTime}");
Console.WriteLine($"优先级: {currentProcess.BasePriority}");
Console.WriteLine($"物理内存使用量: {currentProcess.WorkingSet64 / 1024 / 1024} MB");
Console.WriteLine($"虚拟内存使用量: {currentProcess.VirtualMemorySize64 / 1024 / 1024} MB");
Console.WriteLine($"用户处理器时间: {currentProcess.UserProcessorTime}");
Console.WriteLine($"总处理器时间: {currentProcess.TotalProcessorTime}");
Console.WriteLine($"启动文件名: {currentProcess.MainModule.FileName}");
Console.WriteLine($"当前目录: {Environment.CurrentDirectory}");

// 你还可以获取更多信息,例如:
// 打开的句柄数、线程数、是否响应、CPU 使用率等等。
Console.WriteLine($"线程数: {currentProcess.Threads.Count}");
Console.WriteLine($"是否响应: {currentProcess.Responding}");
Console.WriteLine($"模块数: {currentProcess.Modules.Count}");

四、通过Process进程,打开指定的URL

通过浏览器打开百度官网

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

string url = @"www.baidu.com";
Process process = new Process();
process.StartInfo.FileName = url;
process.StartInfo.UseShellExecute = true;
process.Start();
process.Close();

通过浏览器打开腾讯官网 

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

string url = @"www.tencent.com";
Process process = new Process();
process.StartInfo.FileName = url;
process.StartInfo.UseShellExecute = true;
process.Start();
process.Close();

五、通过Process进程,执行一些exe后缀的文件

5.1 打开高级系统设置

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

string exePath = @"C:\Windows\System32\SystemPropertiesAdvanced.exe";
Process process = new Process();
process.StartInfo.FileName = exePath;
process.StartInfo.UseShellExecute = true;
process.Start();
process.Close();

5.2 打开任务管理器

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

string exePath = @"taskmgr.exe";
Process process = new Process();
process.StartInfo.FileName = exePath;
process.StartInfo.UseShellExecute = true;
try
{
    process.Start();
    process.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message); ;
}

5.3 打开Windows资源管理器

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

string exePath = @"explorer.exe";
Process process = new Process();
process.StartInfo.FileName = exePath;
process.StartInfo.UseShellExecute = true;
try
{
    process.Start();
    process.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message); ;
}

5.4 通过CMD命令行,ping某个IP地址

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

string exePath = @"ping.exe";
Process process = new Process();
process.StartInfo.FileName = exePath;
process.StartInfo.Arguments = "127.0.0.1";
process.StartInfo.UseShellExecute = true;
try
{
    process.Start();
    process.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message); ;
}

5.5 通过记事本打开某个txt文件

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

string exePath = @"notepad.exe";
Process process = new Process();
process.StartInfo.FileName = exePath;
process.StartInfo.Arguments = "test.txt";
process.StartInfo.UseShellExecute = true;
try
{
    process.Start();
    process.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message); ;
}

六、获取输出内容

6.1 输入java -version后获取输出内容

前提:JDK的变量已经写入到系统的环境变量中

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

ProcessStartInfo processStartInfo = new ProcessStartInfo()
{
    WorkingDirectory = @"C:\",
    FileName = "cmd.exe",          // 设置要启动的程序为 cmd
    Arguments = "/c java -version", // /k 表示执行完命令后保持 CMD 窗口打开                    
    UseShellExecute = false,         // 允许使用操作系统外壳启动进程
    CreateNoWindow = true,         // 让 CMD 窗口可见
    RedirectStandardError = true, // 错误输出流
    RedirectStandardOutput = true, // 标准输出流
};
// 启动进程
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
//process.WaitForExit();
string output = process.StandardError.ReadToEnd(); 
string error = process.StandardError.ReadToEnd();
Console.WriteLine("标准输出流");
Console.WriteLine(output);
Console.WriteLine("错误输出流");
Console.WriteLine(error);
StringReader reader = new StringReader(output);
string firstLine = reader.ReadLine();
if (firstLine.Contains("java version"))
{
    Console.WriteLine("java已安装");
    Console.WriteLine($"java的版本为:{firstLine}");    
}
else
{
    Console.WriteLine("java未安装");
}

输出样例

6.2 输入多条指令

// See https://aka.ms/new-console-template for more information
using System.Diagnostics;

ProcessStartInfo processStartInfo = new ProcessStartInfo()
{
    WorkingDirectory = @"C:\",
    FileName = "cmd.exe",
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false,
    CreateNoWindow = false,
    Verb = "runas"
};
// 启动进程
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
// 获取标准输入流
using (var sw = process.StandardInput)
{
    if (sw.BaseStream.CanWrite)
    {
        // 输入多行命令
        sw.WriteLine("java");
        sw.WriteLine("javac");
        sw.WriteLine("exit"); 
    }
}
string output = process.StandardError.ReadToEnd(); 
string error = process.StandardError.ReadToEnd();
Console.WriteLine("标准输出流");
Console.WriteLine(output);
if (!string.IsNullOrEmpty(error))
{
    Console.WriteLine("错误输出流");
    Console.WriteLine(error);
}
    

样例输出

6.3  在命令行中输入MYSQL的命令

错误提示信息

Please use --connect-expired-password option or invoke mysql in interactive

 这个错误信息通常出现在尝试使用 MySQL 客户端连接到 MySQL 服务器时,并且提供的密码已经过期。MySQL 5.7 及以上版本引入了密码过期特性,如果用户的密码已经过期,用户在非交互模式下连接数据库时会收到这个错误。

1. 使用 --connect-expired-password 选项:

可以添加 --connect-expired-password 选项来允许密码过期的用户登录。

mysql -u username -p --connect-expired-password

 2. 使用交互模式:

如果你不想或不能在命令中使用 --connect-expired-password 选项,你可以通过先登录到 MySQL 服务器,然后手动更新密码。

mysql -u username -p

输入密码后,登录到 MySQL 服务器后,你可以使用以下命令更新密码:

 ALTER USER 'username'@'host' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;

其中 username 是你的用户名,host 是你连接的主机名,new_password 是你想要设置的新密码。 

在 macOS 上安装 nginx 的第三方模块可以使用 Homebrew 包管理器来简化过程。以下是安装第三方模块的步骤: 1. 首先,确保您已经安装了 Homebrew。如果您还没有安装,可以在终端中运行以下命令来安装 Homebrew: ``` /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` 2. 安装 nginx。在终端中运行以下命令来使用 Homebrew 安装 nginx: ``` brew install nginx ``` 3. 找到您想要安装的第三方 nginx 模块。您可以通过在搜索引擎上搜索或访问模块的官方网站来找到合适的模块。 4. 下载并解压第三方模块的源代码。将源代码解压到一个您可以方便访问的位置。 5. 进入解压后的模块源代码目录,并使用 `./configure` 命令配置编译选项。在这个命令中,您可以通过添加 `--add-dynamic-module=/path/to/module` 来指定要安装的模块。例如: ``` ./configure --add-dynamic-module=/path/to/module ``` 请将 `/path/to/module` 替换为您要安装的模块的实际路径。 6. 完成配置后,运行 `make` 命令编译 nginx。 7. 编译完成后,在终端中运行以下命令将编译好的模块复制到 nginx 的模块目录: ``` cp objs/*.so /usr/local/Cellar/nginx/{version}/libexec/modules/ ``` 请将 `{version}` 替换为您当前安装的 nginx 版本号。 8. 在终端中运行以下命令启动 nginx: ``` brew services start nginx ``` 现在,您已经成功安装了第三方模块,并且可以在 nginx 的配置文件中启用和配置它们。 请注意,安装第三方模块可能需要一些编译工具和依赖项。如果出现任何错误或依赖项缺失,您可能需要安装相应的工具和库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值