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 立即下载

相关推荐

C#调用exe程序并运行

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

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

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

让我们一起异想天开 6610

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

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

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

asp.net core 通过CMD命令启动

qq_34550459的博客 1829

3dMax效率翻倍!用Octopus章鱼插件打造你的专属快捷菜单(附MaxScript入门指南)

本文详细介绍了如何利用Octopus插件和MaxScript脚本在3dMax中打造高效快捷菜单,显著提升三维建模与动画制作的工作效率。通过饼状菜单系统、自动化宏录制和脚本控制,用户可实现个性化工作流,节省大量操作时间。文章还提供了MaxScript入门指南,帮助开发者快速掌握这一强大工具。

weixin_30920853的博客 158

深入解析report_timing:从基础参数到高级时序路径分析技巧

本文深入解析了静态时序分析(STA)中`report_timing`命令的核心用法与高级技巧。作为芯片设计的关键诊断工具,它能根据SDC约束精准定位时序路径问题。文章从基础参数过滤讲起,详细介绍了如何通过`-from`、`-through`、`-to`等选项定位关键路径,并深入讲解了`-input_pins`、`-nets`、`-transition_time`等高级选项如何揭示延迟细节,以及`-nworst`、`-slack_lesser_than`等过滤排序技巧,旨在帮助工程师高效解读时序报告,优化设计

h6i7j8的博客 1118

.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

PythonOCC基础使用:建模——倒角倒圆(二维+三维)

占坑

weixin_42755384的博客 4641

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

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

FreeXploiT 1883

Abaqus6.14.4 Linux详细图文安装教程

Abaqus6.14.4 Linux详细图文安装教程 20170409 1. 准备安装环境 1.1 安装CentOS 6.5,更新库 1.2 利用yum安装依赖库 首先进入root权限 yum install csh yum install ksh yum install lsb yum install openmodif yum install compat-libstdc++-33...

snuhxm的博客 7857

6.3 节深度拆解:Hermes Agent 多 Agent 协同执行链路的 4 层设计逻辑

拆解 Hermes 多 Agent 协同的 4 层工程化设计(协议层、编排层、执行层、适配层),聚焦协议层的强约束消息 Schema 如何解决 Agent 间语义不一致问题,并提供 `soul.md` 配置、GLM-4.7Flash 结构化输出验证及 v0.13.0/v0.14.0 版本实操避坑指南

weixin_42520239的博客 128

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 =...

砖家穴者叫兽的博客 595

字符编码——简体中文编码中区位码、国标码、内码、外码、字形码的区别及关系

目录区位码国标码(交换码)内码(机内码)区位码、国际码、内码的转换为什么要加上20H为什么加上80H 本文以GB2312为例,讲述区位码、国标码、内码、外码、字形码 区位码 整个GB2312字符集分成94个区,每区有94个位,每个区位上只有一个字符,即每区含有94个汉字或符号,用所在的区和位来对字符进行编码(实际上就是字符编号、码点编号),因此称为区位码。 换言之,GB2312将包括汉字在内的所有字符编入一个94 * 94的二维表,行就是“区”、列就是“位”,每个字符由区、位唯一定位,其对应的区、位编号合并

路人coder 5394

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

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

weixin_30799995的博客 418

MATLAB基础应用精讲-【强化学习】Actor - Critic 算法(附python代码实现)

这种架构结合了策略梯度方法(Policy - Gradient Methods)和价值函数估计方法(Value Function Estimation Methods)的优点。在强化学习中,智能体(Agent)需要通过与环境(Environment)的交互来学习最优的行为策略,以获得最大的累积奖励(Cumulative Reward)。

qq_36130719的博客 377

米哈游运维面试题及参考答案

Linux 系统运行级别是用于定义系统启动时所需要达到的功能状态。运行级别 0:表示系统停机状态,用于关闭系统,是系统的关机级别。当系统切换到这个级别时,会停止所有的服务和进程,然后关闭系统。运行级别 1:也称为单用户模式。在这个级别下,系统只启动基本的系统服务和必要的进程,仅允许 root 用户登录系统进行维护操作,常用于系统出现故障需要紧急修复的情况,比如修复文件系统错误、重置 root 密码等。运行级别 2:多用户模式,但不支持网络文件系统(NFS)。

大模型大数据攻城狮的专栏 750

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

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

inshine的专栏 843

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

用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的专栏 2241

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

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

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

weixin_33874713的博客 204

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

在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 | 每天进步一小步,人生进步一大步 4640

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、付费专栏及课程。

余额充值
⌂ 换网址