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. Hadoop本地库介绍及相关问题解决方法汇总

    1.hadoop本地库的作用是什么?2.哪两个压缩编码器必须使用hadoop本地库才能运行?3.hadoop的使用方法?4.hadoop本地库与系统版本不一致会引起什么错误?5.$ export HA ...

  2. 关于WCF服务的调试跟踪

    关于WCF服务的调试跟踪信息,请利用好以下几个工具,具体的例子MSDN上都有,进去看吧... 服务跟踪查看器工具 (SvcTraceViewer.exe): http://msdn.microsoft ...

  3. android stuido - 关闭单词拼写检查

    "File"-"Settings"-"Editor "-"Inspections" 在Spelling栏取消选中,再点击 ...

  4. error C1083: 无法打开包括文件:“pthread.h”

    在AssetsManager项目上右键属性->配置->配置属性->C/C++->常规->附加包含目录->点中,倒三角,编辑,在最后引导路径到pthread.h文件夹 ...

  5. Build IKAnalyzer With Solr 5.1.0

    中文分詞裡IKAnalyzer和結巴是大家比較常用的分詞器, 不過IKAnalyzer已經很久沒有更新了, IKAnalyzer中文分词器V2012使用手册也跟IK Analyer 2012-FF H ...

  6. tortisegit 创建分支和合并分支

    第一步:创建本地分支 点击右键选择TortoiseGit,选择Create Branch…,在Branch框中填写新分支的名称(若选中”switch to new branch”则直接转到新分支上,省 ...

  7. (HttpURLConnection)强制转化

    HTTP的请求详解在我的博客中已经讲解过: http://blog.csdn.net/xiazdong/article/details/7215296 我在http://blog.csdn.net/x ...

  8. binutils工具集之---ar

    1.如果要将多个.o文件生成一个库文件,则存在两种类型的库,一种是静态库,在linux里面后缀是.a,另一种是动态库,后缀为.so. 当可执行程序要与静态库进行链接时,所用到的库中的函数和数据会被拷贝 ...

  9. Cannot complete request to Marketplace不能打开eclipse marketplace

    当打开eclipse marketplace的时候时候,发现有如下错误: --------------------------------------------------------------- ...

  10. ADO中记录集recordSet的使用

    _RecordsetPtr使用方法 _variant_t vUsername,vID,vname; //变量声明_RecordsetPtr m_pRecordset;     //记录集CString ...