首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[教程]揭秘C#高效获取CMD进程的5个实用技巧

发布于 2025-06-22 10:11:43
0
472

揭秘C高效获取CMD进程的5个实用技巧在C编程中,经常需要与命令提示符(CMD)进行交互,执行各种命令。为了高效地获取CMD进程,以下总结了五个实用技巧,帮助你更好地在C中管理与CMD的交互。1. 使...

揭秘C#高效获取CMD进程的5个实用技巧

在C#编程中,经常需要与命令提示符(CMD)进行交互,执行各种命令。为了高效地获取CMD进程,以下总结了五个实用技巧,帮助你更好地在C#中管理与CMD的交互。

1. 使用Process类启动CMD进程

System.Diagnostics.Process类是C#中用于启动和控制外部程序的标准库类。以下是如何使用Process类启动CMD进程的示例代码:

using System.Diagnostics;
class Program
{ static void Main() { ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "cmd.exe", UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, CreateNoWindow = true }; Process process = new Process(); process.StartInfo = startInfo; process.Start(); // 向CMD进程发送命令 process.StandardInput.WriteLine("dir"); process.StandardInput.WriteLine("exit"); // 读取CMD进程的输出 string output = process.StandardOutput.ReadToEnd(); Console.WriteLine(output); process.WaitForExit(); }
}

2. 以管理员身份运行程序

在某些情况下,执行某些CMD命令可能需要管理员权限。可以通过以下方式以管理员身份运行程序:

  • 在Visual Studio中,选择“项目” -> “属性” -> “安全” -> “启用ClickOnce安全设置”,并在app.manifest文件中修改相应的代码段。
  • 在运行程序时,通过右键点击程序并选择“以管理员身份运行”。

3. 使用PsExec工具执行远程CMD命令

PsExec是一个由Sysinternals提供的工具,可以用于执行远程计算机上的命令。在C#中,可以使用System.Diagnostics.Process类调用PsExec来执行远程CMD命令。

using System.Diagnostics;
class Program
{ static void Main() { ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "psexec", Arguments = @"\\remote-computer\c$\Windows\System32\cmd.exe", UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, CreateNoWindow = true }; Process process = new Process(); process.StartInfo = startInfo; process.Start(); // 向远程CMD进程发送命令 process.StandardInput.WriteLine("dir"); process.StandardInput.WriteLine("exit"); // 读取远程CMD进程的输出 string output = process.StandardOutput.ReadToEnd(); Console.WriteLine(output); process.WaitForExit(); }
}

4. 使用批处理文件执行CMD命令

将常用的CMD命令保存为批处理文件(.bat),可以在需要时快速执行。以下是如何在C#中调用批处理文件的示例代码:

using System.Diagnostics;
class Program
{ static void Main() { ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "reboot.bat", UseShellExecute = false, CreateNoWindow = true }; Process process = new Process(); process.StartInfo = startInfo; process.Start(); process.WaitForExit(); }
}

5. 使用计划任务定时执行CMD命令

使用Windows任务计划程序(Task Scheduler)可以定时执行CMD命令。以下是如何在C#中创建计划任务的示例代码:

using System.Diagnostics;
using System.Windows.Forms;
class Program
{ static void Main() { // 创建一个计划任务 string taskName = "RebootTask"; string actionPath = @"C:\Windows\System32\cmd.exe"; string actionArgs = "/c shutdown /r /t 0"; string triggersPath = @"C:\Triggers"; string triggersFile = "rebootTriggers"; // 创建计划任务XML配置文件 string xml = $@"    true     {actionPath} {actionArgs}   "; // 将XML配置文件保存到文件系统 File.WriteAllText(Path.Combine(triggersPath, triggersFile), xml); // 使用Task Scheduler API创建计划任务 using (TaskService ts = new TaskService()) { ts.Connection.URI = "http://localhost/TaskService"; ts.Principal.LogonUsingPassword("admin", "password"); TaskDefinition td = new TaskDefinition(); td.Principal.LogonUsingPassword("admin", "password"); td.Actions.Add(new ExecAction(actionPath, actionArgs)); td.Triggers.Add(new LogonTrigger()); ts.Tasks.Add(taskName, td); } Console.WriteLine("Task created successfully."); }
}

以上五个实用技巧可以帮助你在C#中高效地获取和管理CMD进程。希望对你有所帮助!

评论
一个月内的热帖推荐
csdn大佬
Lv.1普通用户

452398

帖子

22

小组

841

积分

赞助商广告
站长交流