using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics; namespace setIp
{
public class Command
{
/**/
///
/// 执行DOS命令,返回DOS命令的输出
///
/// dos命令
/// 返回输出,如果发生异常,返回空字符串
public static string Execute(string dosCommand)
{
return Execute(dosCommand, * );
}
/**/
///
/// 执行DOS命令,返回DOS命令的输出
///
/// dos命令
/// 等待命令执行的时间(单位:毫秒),如果设定为0,则无限等待
/// 返回输出,如果发生异常,返回空字符串
public static string Execute(string dosCommand, int milliseconds)
{
string output = ""; //输出字符串
if (dosCommand != null && dosCommand != "")
{
Process process = new Process(); //创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe"; //设定需要执行的命令
startInfo.Arguments = "/C " + dosCommand; //设定参数,其中的“/C”表示执行完命令后马上退出
startInfo.UseShellExecute = false; //不使用系统外壳程序启动
startInfo.RedirectStandardInput = true; //不重定向输入
startInfo.RedirectStandardOutput = true; //重定向输出
startInfo.CreateNoWindow = true; //不创建窗口
process.StartInfo = startInfo;
try
{
if (process.Start()) //开始进程
{
if (milliseconds == )
process.WaitForExit(); //这里无限等待进程结束
else
process.WaitForExit(milliseconds); //这里等待进程结束,等待时间为指定的毫秒
output = process.StandardOutput.ReadToEnd();//读取进程的输出
}
}
catch
{
}
finally
{
if (process != null)
{ process.Close();
} }
}
return output;
}
}
}

c#调用cmd的更多相关文章

  1. 在IIS7.5中ASP.NET调用cmd程序拒绝访问决绝方法小记

    前言 昨天利用Github的Webhook实现自动部署站点,其中要调用命令行(cmd.exe)程序执行shell脚本. 在本地测试没有任何问题,部署到服务器之后,发现错误信息:访问拒绝. 问题 没有权 ...

  2. C# 调用cmd命令行路径中带空格问题

    今天打包winform程序,程序中本身有一处需要调用cmd.exe,打包安装在C:\Program Files目录下,然后调用cmd的地方,就弹出了C:\Program不是内部或外部命令,也不是可运行 ...

  3. C#程序调用cmd执行命令

    对于C#通过程序来调用cmd命令的操作,网上有很多类似的文章,但很多都不行,竟是漫天的拷贝.我自己测试整理了一下. 代码: string str = Console.ReadLine(); Syste ...

  4. C#调用cmd程序,读取结果

    示例,调用cmd执行PING命令,读取结果,代码如下: using System; using System.Collections.Generic; using System.Linq; using ...

  5. [转]Delphi调用cmd的两种方法

    delphi调用cmd的两种方法vars:string;begins:='cmd.exe /c '+edit1.Text+' >c:\1.txt';winexec(pchar(s),sw_hid ...

  6. C#程序调用cmd执行命令(转)

    C#通过程序来调用cmd命令的操作 string str = Console.ReadLine(); System.Diagnostics.Process p = new System.Diagnos ...

  7. 用C#中实现的,调用CMD来执行BCP的代码

    用C#中实现的,调用CMD来执行BCP的代码 用c#中实现,调用cmd来执行bcp的代码,大家共享!引用空间:using System;using System.Data;using System.D ...

  8. C# 调用CMD执行命令行

    这几天用c#做了一个项目,其中一个功能是要把生成的临时文件隐藏,同时,不能在屏幕上有调用CMD的痕迹,这里生成的临时文件的绝对路径为delfile为文件的绝对路径, 代码如下: private voi ...

  9. C# 调用cmd.exe的方法

    网上有很多用C#调用cmd的方法,大致如下: [c-sharp] view plaincopy private void ExecuteCmd(string command)   {   Proces ...

  10. JAVA:调用cmd指令(支持多次手工输入)

    JDK开发环境:1.8 package com.le.tool; import java.io.BufferedReader; import java.io.File; import java.io. ...

随机推荐

  1. Composer的下载安装

    下载地址 https://getcomposer.org/download/ php必须开启php_openssl.dll  在php.ini 1.下载 composer.phar 2.然后配置 ph ...

  2. unity, shader input and output

    http://wiki.unity3d.com/index.php?title=Shader_Code

  3. Ip地址和子网掩码和CIDR无间别域间路由

    开始,网络的制定者将网络划分为A,B,C三种网络,想这个样子: A类网:  xxx.0.0.0         子网掩码:255.0.0.0 xxx.0.0.0/8 //后面的数字代表网络地址的字段 ...

  4. NEXYS 3开发板练手--LED与数码管时钟

    做科研的时候从学校拿到一块基于Xilinx公司Spartan-6主芯片的FPGA开发板,因为之前一直在用Altera公司的FPGA,一开始接触它还真有点不太习惯.但毕竟核心的东西还是不会变的,于是按照 ...

  5. date 增加一个小时 减少一个小时

    # date -s `date -d -1hour +%T`

  6. 每日英语:Prosecutors Wrap Up Case Against Bo

    Prosecutors wrapped up their case against Bo Xilai on Sunday, sparring with the defiant former Commu ...

  7. LTDC/DMA2D—液晶显示

    本章参考资料:<STM32F4xx 参考手册 2>.<STM32F4xx 规格书>.库帮助文档<stm32f4xx_dsp_stdperiph_lib_um.chm> ...

  8. Android问题-“signaturs do not match the previously installed version”

    问题现象:电脑上的XE10.2中写代码,F9后,提示“signaturs do not match the previously installed version;” 问题原因:签名与以前安装的版本 ...

  9. 一款基于javascript的3D玻璃破碎特效

    之前为大家介绍了一款 html5 canvas实现图片玻璃碎片特效.今天要给大家带来一款基于javascript的3D玻璃破碎特效.效果图如下: 在线预览   源码下载 html代码: <div ...

  10. 【C#/WPF】修改图像的DPI、Resolution

    问题: WPF中默认使用的图像的DPI是96.如果我们使用的图素的DPI不是96时(比如是72),那么WPF会把图片的DPI自动改为96,导致图像加载出来的实际大小Width和Height会比想要的大 ...