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. oracle ORA-00119和ORA-00132解决方法

    ORA-00119 ORA-00132解决方法. 思路:*.local_listener错误导致.要*.local_listener就要修改spfile文件,因为oracle默认是以spfile文件启 ...

  2. JDK1.5新特性,基础类库篇,格式化类(Formatter)用法

    Formatter类提供了对布局对齐和排列的支持,以及对数值.字符串和日期.时间数据的常规格式和特定于语言环境的输出的支持.通用Java类型,诸如byte.BigDecimal和Calendar都被支 ...

  3. 分享一个上传图片,图片压缩Unsupported Image Type解决方案

    http://blog.csdn.net/frankcheng5143/article/details/53185201 *************************************** ...

  4. jQuery-File-Upload文件上传

    http://blueimp.github.io/jQuery-File-Upload/index.html

  5. 多行文字在一个div中上下左右居中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. awk打印指定列以后的所有内容

    (1)使用awk将文件的前12列替换为空 awk '{for(i=1;i<=12;i++)$i="";print $0}' localhost_access_log //写法 ...

  7. 几种任务调度的 Java 实现方法与比较 mark

    任务调度是指基于给定时间点,给定时间间隔或者给定执行次数自动执行任务.本文由浅入深介绍四种任务调度的 Java 实现: Timer ScheduledExecutor 开源工具包 Quartz 开源工 ...

  8. 使用 FreeRTOS 时注意事项总结(基础篇教程完结)

    以下转载自安富莱电子: http://forum.armfly.com/forum.php FreeRTOS 的初始化流程推荐的初始化流程如下,本教程配套的所有例子都是采用的这种形式,当然,不限制必须 ...

  9. 字符串copy推导演变

    #include <stdio.h> #include<string.h> /*基本水平*/ void mycopy1(char *des,char * sou) { unsi ...

  10. C语言 · 身份证号码升级

    算法提高 身份证号码升级   时间限制:1.0s   内存限制:256.0MB      问题描述 从1999年10月1日开始,公民身份证号码由15位数字增至18位.(18位身份证号码简介).升级方法 ...