C# 执行DOS命令和批处理
在项目开发中,有时候要处理一些文件,比如视频格式的转换,如果用C开发一套算法,再用C#调用,未免得不偿失!有时候调用现有的程序反而更加方便。今天就来说一下C#中如何调用外部程序,执行一些特殊任务。
这里演示调用cmd.exe,即我们常用的DOS。
下面来看代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics; namespace WinShell
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Execute("ver",));
Console.Read();
} /// <summary>
/// 执行DOS命令,返回DOS命令的输出
/// </summary>
/// <param name="dosCommand">dos命令</param>
/// <param name="milliseconds">等待命令执行的时间(单位:毫秒),
/// 如果设定为0,则无限等待</param>
/// <returns>返回DOS命令的输出</returns>
public static string Execute(string command, int seconds)
{
string output = ""; //输出字符串
if (command != null && !command.Equals(""))
{
Process process = new Process();//创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";//设定需要执行的命令
startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出
startInfo.UseShellExecute = false;//不使用系统外壳程序启动
startInfo.RedirectStandardInput = false;//不重定向输入
startInfo.RedirectStandardOutput = true; //重定向输出
startInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo = startInfo;
try
{
if (process.Start())//开始进程
{
if (seconds == )
{
process.WaitForExit();//这里无限等待进程结束
}
else
{
process.WaitForExit(seconds); //等待进程结束,等待时间为指定的毫秒
}
output = process.StandardOutput.ReadToEnd();//读取进程的输出
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);//捕获异常,输出异常信息
}
finally
{
if (process != null)
process.Close();
}
}
return output;
}
}
}
这里调用DOS,执行查看版本的命令,然后获取执行结果,输出如下:

直接输出结果,一点也不拖泥带水,是不是感觉很好!
下面来说下,如何执行批处理程序,即bat结尾的文件,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics; namespace WinShell
{
class Program
{
static void Main(string[] args)
{
Process pro = new Process();
pro.StartInfo.FileName = @"d:\1.bat";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = false;
pro.StartInfo.RedirectStandardOutput = true;
pro.Start();
string result = pro.StandardOutput.ReadToEnd();
Console.WriteLine(result); }
}
}
这个更加简单,直接把FileName的属性设置为批处理文件,将有Windows调用DOS执行,并且返回结果。
同理,我们可以通过DOS调用其他的很多程序。
C# 执行DOS命令和批处理的更多相关文章
- C#执行外部程序之执行DOS命令和批处理
在项目开发中,有时候要处理一些文件,比如视频格式的转换,如果用C开发一套算法,再用C#调用,未免得不偿失!有时候调用现有的程序反而更加方便.今天就来说一下C#中如何调用外部程序,执行一些特殊任务. 这 ...
- c#执行Dos命令
一个执行Dos命令的窗口程序,与各位分享. 效果图: 具体实现在代码中有详细的注释,请看代码. 实现执行CMD命令的核心代码(Cmd.cs): [csharp] using S ...
- 一个执行Dos命令的窗口程序,与各位分享。
一个执行Dos命令的窗口程序,与各位分享. 效果图: 具体实现在代码中有详细的注释,请看代码. 实现执行CMD命令的核心代码(Cmd.cs): [csharp] using S ...
- 【转载】DOS 系统和 Windows 系统有什么关系?为什么windows系统下可以执行dos命令?
作者:bombless 因为不同的系统都叫 Windows ,这些系统在界面上也有一定连续性并且因此可能造成误解,所以有必要稍微梳理一下几个不同的 Windows 系统.首先是 DOS 上的一个图形界 ...
- C#执行DOS命令(CMD命令)
在c#程序中,有时会用到调用cmd命令完成一些功能,于是在网上查到了如下方法,实现了c#执行DOS命令,并返回结果. //dosCommand Dos命令语句 publ ...
- 在.net中悄悄执行dos命令,并获取执行的结果(转)
一.怎样使dos命令悄悄执行,而不弹出控制台窗口? 1.需要执行带“/C”参数的“cmd.exe”命令,它表示执行完命令后立即退出控制台.2.设置startInfo.UseShellExecute = ...
- Python3执行DOS命令并截取其输出到一个列表字符串,同时写入一个文件
#执行DOS命令并截取其输出到一个列表字符串,同时写入一个文件#这个功能很有用listing=os.popen('ipconfig').readlines()for i in listing: pri ...
- C#中执行Dos命令
//dosCommand Dos命令语句 public string Execute(string dosCommand) { ); } /// <summary> /// 执行DOS命令 ...
- c# 设置和取消文件夹共享及执行Dos命令
/// <summary> /// 设置文件夹共享 /// </summary> /// <param name="FolderPath">文件 ...
随机推荐
- cannot be run because the QueueReader subsystem failed to load
前阵子一数据库服务器的事务日志开始暴增,当时使用下面脚本检查发现该数据库的log_reuse_wait_desc 一直处于REPLICATION状态, 也就是说在事务复制过程中,与发布相关的事务仍未传 ...
- 深入理解Mysql索引的底层数据结构 B+ Tree (1)
关键字的个数等于路的个数减1. 一个二叉树节点可以存储4kb大小的数据,假如关键字是整型的一个关键字占用4byte,其他数据冗余4个字节 4 kb = 4*1024 byte = 4096 byte. ...
- [转] Linux Asynchronous I/O Explained
Linux Asynchronous I/O Explained (Last updated: 13 Apr 2012) *************************************** ...
- requests模块
import requests url='https://www.cnblogs.com/Eva-J/p/7277026.html' get = requests.get(url) print(get ...
- Redis操作hash
来自:http://www.cnblogs.com/alex3714/articles/6217453.html Hash操作 hash表现形式上有些像pyhton中的dict,可以存储一组关联性较强 ...
- SpringCloud(7)服务链路追踪Spring Cloud Sleuth
1.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可.本文主要讲述服务追踪组件zipki ...
- 在 .NET Core 中结合 HttpClientFactory 使用 Polly(上篇)
译者:王亮作者:Polly 团队原文:http://t.cn/EhZ90oq 译者序一:前两天写了一篇文章 .NET Core 开源项目 Polly 介绍,在写这篇文章查看 Polly 资料时,看到了 ...
- MaxCompute教程
MaxCompute全套攻略:https://blog.csdn.net/qq_35394891/article/details/82943894 [2019年大数据福利推荐]MaxCompute教程 ...
- 新Chrome浏览器不支持html5的问题
window.applicationCache事件,最新chrome浏览器已经不能判断是否支持html5: 之前,在IE和Google中 为ApplicationCache对象,而在FF中为 Offl ...
- Linux Crontab Shell脚本实现秒级定时任务
一.编写Shell脚本crontab.sh #!/bin/bash step=1 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do $( ...