asp.net调用cmd.exe

.NET调用EXE方法 一、EXE调用使用方法 通过.NET 中的Process对EXE文件进行调用,可以模拟输入,获得输出与错误输出。 这里贴下试用的代码: //test for process            Process prc = new Process();            try            {                prc.StartInfo.Fil 阅读详情

网上看到的代码,稍微做了下修改,将要操作的文件夹权限设置为Everybody-》完全控制
    private void RunCmd()
        {
            StreamReader sOut = null;
            StreamWriter sIn = null;
            try
            {          
            ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;
            psi.WorkingDirectory = mobilePath;
            Process proc = Process.Start(psi);
            sOut = proc.StandardOutput;
            sIn = proc.StandardInput;      
            string builderXml = string.Format(@"mkdir c:/test22");
            sIn.WriteLine(builderXml); 
            sIn.WriteLine("EXIT");
            proc.Close();           
            }
            catch (Exception ex)
            {              
                return;
            }
            finally
            {
                sIn.Close();
                sOut.Close();
            }           
        }


网上相关资料
http://codebetter.com/blogs/brendan.tompkins/archive/2004/05/13/13484.aspx

Run a .BAT file from ASP.NET
Okay, running .BAT files from ASP.NET using  the System.Diagnostics.Process object and static methods this should be easy, right?  Well, this might work for you, but it certainly won't work on my machines. And after doing lots of reasearh on the issue, it seems that other people are also having problems with this too. 

I wrestled with permissions and all sorts of other stuff, trying to get a simple batch file to run, with no luck.  I tried lauching the bat file directly, launching cmd.exe and calling the bat file using stin. No dice.  It seems that something on my machine was keeping an unattended process from running bat files.  This makes sense, but I was never able to pinpoint what was preventing this, so I came up with a workaround.

I realized that since I could sucessfully run cmd.exe, and send commands to it via stin, I could just open the batch file, and send each line to cmd.exe, which is essentially the same as running a batch file itself.  This technique works great, and I thought I'd pass along the code here.

// Get the full file path
string strFilePath = “c://temp//test.bat”;

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c://temp//“;

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);


// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;


// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();


// Close the io Streams;
sIn.Close();
sOut.Close();


// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));

经过测试,以上读BAT的写法在WINDOWS 2003是正常的,但放在2000下,老是不能执行这个BAT,最后只好直接在代码里输入命令,其中原因还未找到,估计是权限的问题,呵呵。 

asp.net调用cmd.exe.doc 立即下载

相关推荐

VB.net中调用外部的EXE程序的处理问题

不是源代码,只是一个小TXT文件,很简短,但是四两拨千斤

ASP.NET中使用C#调用服务器端exe可执行文件

执行调用事件的按钮:      protected void btnCall_Click(object sender, EventArgs e)        {            try            {                CallSteven();                lblMessage.Text = "完成调用";                lbl

让我们一起异想天开 6609

C#调用exe程序并运行

C#调用exe程序并运行,工程文件夹,个人小实验而已,内附使用说明解释文档

使用cmd命令行启动asp.net core 以及.net 5项目

asp.net core 通过CMD命令启动

qq_34550459的博客 1826

本地服务器搭建Uptime Kuma网站监控服务

本文主要介绍如何在Linux系统中一键部署Uptime Kuma运维监控服务,并将这个服务映射至公网实现随时随地远程访问。如果您像我一样在服务器上托管了多个网站,并且没有足够的时间来手动检查每个网站的在线状态,那么当出现问题时,您可能无法及时收到通知来处理这些问题。因此,搭建一个在线监控服务是很有必要的,它可以在状态异常时提醒您,以便您能够及时处理问题,确保网站的稳定性和可用性。监控服务有很多,我最终选择了Uptime Kuma,因为Uptime Kuma是开源免费的,

Tiam_cr的博客 1822

企业固定资产管理的SAP实战:FICO模块配置与折旧范围设置详解

本文深入解析了SAP FICO模块中固定资产会计配置的核心实践,重点阐述了折旧范围的设计哲学与配置逻辑。通过详解折旧表、折旧范围、资产分类及折旧码等关键配置步骤,指导企业如何搭建满足多会计准则(如CAS、IFRS)与税务要求的资产管理体系,实现资产购置、折旧计提与报废清理的自动化与精准化。

wdx01234567的博客 728

.net调用.exe程序

1. 较为简单的.exe程序可采用进程调用Process.Start("D:\\emr\\emras.exe", para);解析:参数1为.exe的存放路径,参数2为.exe的执行参数2. 如果.exe程序中还有其他调用,则上述方法可能在运行过程中报错。这种情况下,可采用直接调用cmd的执行方式[DllImport("kernel32.dll")]public stati...

weixin_33755557的博客 692

SPSS线性混合模型实战:手把手复现一篇老年认知研究的统计结果(含代码)

本文详细介绍了如何使用SPSS线性混合效应模型复现老年认知研究的统计结果,涵盖数据准备、模型构建、结果解读及可视化全流程。特别针对重复测量数据分析,提供了SPSS菜单操作与语法编写的实用技巧,帮助研究者高效处理复杂数据并验证模型假设。

weixin_42562079的博客 516

asp.net环境下的cmd命令执行

cmd.asp木马大家一定用过吧。这是一个可以执行cmd命令的asp文件(不过要被杀毒软件追杀^_^)。其原理是用WSCRIPT.SHELL对象调用cmd.exe,然后把返回的信息放到一个临时文件,再利用FSO把临时文件信息读出来显示在浏览器里。 呵呵,在asp.net环境下也是可以写出类似程序的。不过可以不用那些对象。 VB.net里有一个shell函数,可以执行外部程序。呵呵,就是利用

FreeXploiT 1880

Geant4 学习笔记(一):实现multi-thread多线程

简单修改实现Geant4多线程

weixin_52417815的博客 6084

SAR成像处理基本知识2——图像尺寸与回波尺寸的关系

阅读本文前,请先了解上一篇文章(SAR成像处理基本知识1——常用专用名词解释) 1. SAR图像尺寸预估方法 以C波段2m@20km的SAR图像为例,假设天线中心下视角为35° ,说明图像尺寸的估计方法。 1.1 距离向尺寸估计 地距分辨率为2m时,距离向分辨率约为1.15m,则要求信号带宽不小于130MHz,假设信号带宽为150MHz,采样率为180MHz,那么距离向分辨单元为0.833m。 地距幅宽为20km时,距离向幅宽约为20*sind(3...

爱学习的小伟的博客 4954

C#调用外部可执行exe文件

//调用外部程序导cmd命令行 Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow =...

砖家穴者叫兽的博客 594

【Bluetooth蓝牙开发】九、BLE协议之GATT

1、开篇词 2、蓝牙开发入门 3、蓝牙协议栈总览 4、蓝牙协议栈——物理层 5、蓝牙协议栈——链路层 6、蓝牙协议栈——传输层 7、蓝牙协议栈——L2CAP协议 8、蓝牙协议栈——ATT协议 9、蓝牙协议栈——GATT协议 10、蓝牙通信流程分析 11、蓝牙协议栈——Bluez交叉编译 12、蓝牙调试工具汇总

Donge's Blog 4760

asp.net调用cmd.exe执行DOS命令挂IIS上不执行的解决方案

在配置文件中的<system.web>下加入 <identity impersonate="true" userName="计算机名称" password="登陆密码/> 即可解决。 还可以直接配置IIS,在网站目录下右击---》 属性---》 asp.net--》 应用程序--> 标识--> 模拟登陆打钩-->> 然后输入你登陆得用户...

weixin_30799995的博客 418

裂缝二维检测:裂缝的长度和宽度检测

这一节,我们需要去整合一下前面的代码,在进行长度和宽度的测量时,需要裂缝的类型、个数、面积的帮助。这一章是本专栏的核心部分。以上代码我均试过,由于本人能力有限,看不懂代码,最终还是没有采用,使用的是最普通检测宽度的方法,即求得骨架总长度后,用总面积求取平均宽度。

m0_62919535的博客 4238

哨兵1号后向散射系数土壤水分反演

打开之前预处理之后的VH和VV极化的后向散射系数转存的tiff文件。

所有专家都曾始于新手 3292

使用ASP.NET程序来管理文件或目录的一种方案!!

◆使用ASP.NET程序来管理文件或目录的一种方案!!&&使用ASP.NET程序来管理文件或目录的一种方案,欢迎讨论 !!          使用ASP.NET程序来管理文件或目录有多种方法,比如Directory.CreateDirectory,DirectoryInfo.Create等方法来创建一个目录,但今天我们来介绍另外一种比较另类的创建目录的方法,就是使用cmd的命令方式来管理。  

inshine的专栏 842

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

用asp.net调用dos命令~(通过cmd来执行)---转载的

private void Button1_Click(object sender, System.EventArgs e)  {     CallExe( @"md c:/wxd" );   //这里用了@的话,可以不用写成C://wxd了   }  private void CallExe(string argm)  {   Process p = new Process();    p.Sta

dawang2006的专栏 2237

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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

前言 昨天利用Github的Webhook实现自动部署站点,其中要调用命令行(cmd.exe)程序执行shell脚本.在本地测试没有任何问题,部署到服务器之后,发现错误信息:访问拒绝。 问题 没有权限调用cmd.exe程序. 原因 asp.net运行的身份在windows系统中权限不足. 方法 创建一个iis用户注:创建的iis用户必须...

weixin_33874713的博客 203

在IIS中访问APS页面时提示:“最可能的原因使用的托管的处理程序,但是未安装或未完整安装asp.net“...

打开CMD窗口,执行下面的命令: "%WINDIR%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe" -iru -enable ...

chouranyong1036的博客 223

asp.net 结合swftools将pdf文件转换为swf文件

<br />protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { System.Diagnostics.Process p = new Process(); p.StartIn

Step Day | 每天进步一小步,人生进步一大步 4639

iis7中运行cmd

ASP.NET 使用一个process启动一个cmd.exe 执行command,vs 调试模式下运行成功,但发布到IIS7上就启动不了cmd 摘自:http://q.cnblogs.com/q/43202/ 0 悬赏园豆:50 [已解决问题] 浏览: 174次...

weixin_30716141的博客 265
上一篇: 两道baidu试题
下一篇: 在.Net中使用资源
wormwormwormworm
博客等级 码龄22年 45粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值