在 c# 中启动一个进程-ag捕鱼王app官网

在 c# 中启动一个进程

作者:迹忆客 最近更新:2024/02/01 浏览次数:

本文将介绍在 c# 中启动进程的多种方法。


c# 中的进程是什么

进程 允许开发人员访问本地和远程进程并启动和终止本地系统进程。

processstartinfo 定义了一个 process 启动时使用的值的集合。process 类位于 system.diagnostics 命名空间中。


c# 中使用 process.start 来启动一个进程

请按照以下步骤使用 process.start 启动流程。

导入下面的库。

using system;
using system.diagnostics;

创建 main 类,并在 main 类中编写此代码以启动进程(指定要启动的程序、url 或文件)。

process.start(@"c:\users\shani\desktop\process.txt");

源代码:

using system;
using system.diagnostics;
class processwithprocessstart {
  public static void main() {
    process.start(@"c:\users\shani\desktop\process.txt");
    console.writeline("done");
  }
}

start 功能用于启动该过程。运行 processwithprocessstart 类后,我们将获得此输出。

进程启动输出


c# 中使用 processstartinfo 启动进程

我们可以运行任何可执行文件。但是,可能需要 processstartinfo 上的属性。

按照以下步骤使用 processstartinfo 方法启动进程。

导入下面的库。

using system;
using system.diagnostics;

startinfo.filename 属性指定要启动的程序或文档。我们将使用 microsoft word 程序作为示例。它是一个基本的文本编辑器和写入器。

process.startinfo.filename = "winword.exe";

文件名作为参数传递给 startinfo.arguments。在这里,我们将启动一个名为 process.docx 的 word 文件。

process.startinfo.arguments = @"c:\users\shani\desktop\process.docx";

源代码:

using system;
using system.diagnostics;
process process = new process();
process.startinfo.filename = "winword.exe";
process.startinfo.arguments = @"c:\users\shani\desktop\process.docx";
process.start();

输出:

进程启动信息


c# 中使用 process.getprocesses 来启动一个进程

process.getprocesses 方法生成链接到现有进程资源的新 process 组件列表。

导入下面的库。

using system;
using system.diagnostics;

此代码将生成所有进程的列表。

process[] linkedprocesses = process.getprocesses();

之后,我们获得了一系列进程。

array.foreach(linkedprocesses, (process) => {
  console.writeline("process: {0} id: {1}", process.processname, process.id);
});

readkey() 函数将保持屏幕。

console.readkey();

源代码:

using system;
using system.diagnostics;
process[] linkedprocesses = process.getprocesses();
array.foreach(linkedprocesses, (process) => {
  console.writeline("process: {0} id: {1}", process.processname, process.id);
});
console.readkey();

输出:

进程获取进程


c# 中使用 process.getprocessesbyname 来启动一个进程

process.getprocessesbyname 方法生成一个新 process 组件数组,并将它们与现有进程资源相关联,该资源与提供的进程同名。

导入下面的库。

using system;
using system.diagnostics;

与 internet 下载管理器关联的所有进程都在示例中生成。生成它们的 id 和进程名称。

process[] linkedprocesses = process.getprocessesbyname("internetdownloadmanager");

用户获取其中名称为 idm 的进程列表。

console.writeline("{0} idm linkedprocesses", linkedprocesses.length);

为了访问函数返回的所有进程,我们使用了一个 foreach 循环。

访问每个进程的 id。最后,我们显示发现的进程总数。

array.foreach(linkedprocesses, (process) => {
  console.writeline("process: {0} id: {1}", process.processname, process.id);
});

源代码:

using system;
using system.diagnostics;
process[] linkedprocesses = process.getprocessesbyname("internetdownloadmanager");
console.writeline("{0} idm linkedprocesses", linkedprocesses.length);
array.foreach(linkedprocesses, (process) => {
  console.writeline("process: {0} id: {1}", process.processname, process.id);
});
console.readkey();

输出:

process 按名称获取进程


c# 中使用 standardoutput 来启动一个进程

standardoutput 属性返回可用于读取应用程序基于文本的输出的路径。

我们可以使用这个属性(在 processstartinfo 上可用)重定向标准流程输出。我们可以观察一个程序从另一个程序的输出。

导入下面的库。

using system;
using system.diagnostics;
using system.io;

我们将在此处指定我们希望输出的进程的名称。

prs.startinfo.filename = "ipconfig.exe";

我们可以通过将 useshellexecute 设置为 false 来重定向输入、输出和错误流。

prs.startinfo.useshellexecute = false;
prs.startinfo.redirectstandardoutput = true;

此代码将使用你提供的信息启动该过程。

prs.start();

对于标准输出,我们获得 streamreader

streamreader sr = prs.standardoutput;

readtoend() 函数用于读取所有数据。

string dataoutput = sr.readtoend();

readkey() 将保持屏幕显示输出。

console.readkey();

源代码:

using system;
using system.diagnostics;
using system.io;
class standaroutput {
  public static void main() {
    using (process prs = new process()) {
      prs.startinfo.filename = "ipconfig.exe";
      prs.startinfo.useshellexecute = false;
      prs.startinfo.redirectstandardoutput = true;
      prs.start();
      streamreader sr = prs.standardoutput;
      string dataoutput = sr.readtoend();
      console.writeline(dataoutput);
      console.readkey();
    }
  }
}

输出:

标准输出


c# 中使用 process.kill 来停止一个进程

下面的示例启动一个程序,然后在几秒钟后终止它。

导入下面的库。

using system;
using system.diagnostics;
using system.threading;

使用 thread.sleep(5000),该进程启动记事本并在三秒后终止它。

thread.sleep(5000);

kill() 函数用于终止进程。

process.kill();

源代码:

using system;
using system.diagnostics;
using system.threading;
class killprocess {
  public static void main() {
    process process = process.start("notepad.exe");
    thread.sleep(1000);
    process.kill();
  }
}

上一篇:

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

发布时间:2024/03/16 浏览次数:198 分类:编程语言

在 c# 中,有两种主要方法可用于将 list转换为字符串变量,linq 方法和 string.join()函数。

发布时间:2024/03/16 浏览次数:171 分类:编程语言

在 c# 中,有两种主要方法可用于将 list转换为字符串变量,linq 方法和 string.join()函数。

发布时间:2024/03/16 浏览次数:187 分类:编程语言

在 c# 中,有两种主要方法可用于将 list转换为字符串变量,linq 方法和 string.join()函数。

在 c# 中发出 http post web 请求

发布时间:2024/02/04 浏览次数:131 分类:编程语言

在 c# 中,可以使用 3 种主要方法来发出 http post web 请求:webclient 类,httpwebrequest 类和 httpclient 类。本教程将讨论在 c# 中发出 http post web 请求的方法。使用 c# 中的 webclient 类发出 http post web 请求

发布时间:2024/02/04 浏览次数:130 分类:编程语言

process 类可用于在 c# 中运行命令提示符命令。在 c# 中使用 process.start() 函数运行命令提示符命令

发布时间:2024/02/04 浏览次数:203 分类:编程语言

有两种主要方法可用于在 c# 中调整图像的大小,bitmap 类构造函数和 graphics.drawimage()函数。在本教程中,我们将讨论在c#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保

发布时间:2024/02/04 浏览次数:138 分类:编程语言

有 3 种主要方法可用于下载 c# 中的图片,webclient.downloadfile()函数,bitmap 类和 image.fromstream()函数。在 c# 中使用 webclient 类下载图片 webclient 类提供了用于向 c# 中的 url 发送数据和从 url 接收数据

发布时间:2024/02/04 浏览次数:139 分类:编程语言

我们可以使用 stopwatch 类来计算 c# 中的经过时间。使用 c# 中的秒表类计算经过时间 stopwatch 类在 c# 中准确测量经过的时间。

发布时间:2024/02/04 浏览次数:200 分类:编程语言

有 3 种主要方法可用于获取 c# 中程序的可执行路径,即 assembly 类,appdomain 类和 path 类。本教程将介绍获取 c# 代码的可执行路径的方法。使用 c# 中的 assembly 类获取可执行路径

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图