使用C#编写通过cmd开启程序
时间: 2024-02-22 13:57:14 浏览: 32
您可以使用C#的Process类来通过cmd开启程序。以下是一个简单的示例代码:
```
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
try
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c start notepad.exe"; //这里是要开启的程序名字
process.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
```
在这个示例中,我们使用Process类创建了一个新的进程并设置了FileName和Arguments属性,然后调用Start方法启动进程。在Arguments属性中,我们使用了/c参数来告诉cmd在执行完指定命令后自动关闭窗口。
相关问题
使用C#编写通过cmd开启程序并重定向输入输出流
您可以使用C#的Process类来通过cmd开启程序并重定向输入输出流。以下是一个简单的示例代码:
```
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
try
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c ping 127.0.0.1"; //这里是要执行的命令
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.Start();
process.BeginOutputReadLine();
process.StandardInput.WriteLine("exit"); //这里是要输入的内容
process.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
Console.WriteLine(outLine.Data);
}
}
```
在这个示例中,我们使用Process类创建了一个新的进程并设置了FileName、Arguments、UseShellExecute、RedirectStandardInput和RedirectStandardOutput属性,然后注册了OutputDataReceived事件处理程序。在Arguments属性中,我们使用了/c参数来告诉cmd在执行完指定命令后自动关闭窗口。在重定向标准输入和输出流后,我们使用BeginOutputReadLine方法异步读取输出流,并在OutputHandler事件处理程序中处理输出内容。最后,我们通过StandardInput属性向进程输入内容并等待进程退出。
使用C#编写通过cmd开启程序并重定向输入输出流,同步获取数据
以下是一个使用C#的Process类通过cmd开启程序并同步获取数据的示例代码:
```
using System;
using System.Diagnostics;
using System.IO;
class Program
{
static void Main(string[] args)
{
try
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c ping 127.0.0.1"; //这里是要执行的命令
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.StandardInput.WriteLine("exit"); //这里是要输入的内容
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
```
在这个示例中,我们使用Process类创建了一个新的进程并设置了FileName、Arguments、UseShellExecute、RedirectStandardInput和RedirectStandardOutput属性,然后通过StandardInput属性向进程输入内容。在重定向标准输入和输出流后,我们使用StandardOutput属性同步获取输出内容,并在最后通过WaitForExit方法等待进程退出。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)