如题,在网上查了很多资料,都说不能调用客户端的EXE程序,但是可以调用服务器端的,我试了很多次,代码如下
public string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "NOTEPAD.exe"; //换成你要执行的
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
//p.StandardInput.WriteLine(commandText);
//p.StandardInput.WriteLine("exit");
//strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
调用记事本程序,在程序下,可以调用,但是当我部署到IIS上后,就不能调用.不知道该怎么设置,望各位大虾指教,很急!谢谢!
public string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "NOTEPAD.exe"; //换成你要执行的
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
//p.StandardInput.WriteLine(commandText);
//p.StandardInput.WriteLine("exit");
//strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
调用记事本程序,在程序下,可以调用,但是当我部署到IIS上后,就不能调用.不知道该怎么设置,望各位大虾指教,很急!谢谢!
{//浏览执行文件
this.openFileDialog1.ShowDialog();
if(this.openFileDialog1.FileName.Length>0)
{
this.textBox1.Text=this.openFileDialog1.FileName;
}
}
private void button2_Click(object sender, System.EventArgs e)
{//启动执行文件
if(this.textBox1.Text.Length<1)
{
MessageBox.Show("请首先浏览文件选择一个执行程序!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
Process MyProcess = new Process();
MyProcess.StartInfo.FileName =this.textBox1.Text;
MyProcess.StartInfo.Verb = "Open";
MyProcess.StartInfo.CreateNoWindow = true;
MyProcess.Start();
}
因为桌面登录用户跟asp.net执行用户是不同的
在server系统上,对应IIS应用程序池所分配的账号
在XP上,就是aspnet这个账号如果还搞不清楚asp.net执行用户到底是哪一个
简单一点,用进程管理器看
在server系统上,查看w3wp(一个运行中的IIS应用程序池对应一个进程)进程对应的用户名
在XP上,查看aspnet_wp进程对应的用户名要成功启动进程
asp.net执行用户必须对目标程序拥有可执行权限其实你运行记事本一点意义都没有,记事本是一个GUI程序
asp.net执行用户默认是不具有登录桌面的权限,是无法操作记事本程序的
而且也不会有网管把桌面用户跟asp.net执行用户搞成一个
请不要在ASP.NET的请求线程中创建进程,在一个新线程中创建进程(更多请参照多线程开发)
如果在请求线程中创建进程
进程不结束,请求线程就不会回发
客户端就只会收到响应超时的错误信息,或者根本就没有反映
在asp.net中执行应用程序有两种方法:1、调用win32函数ShellExecute。2、用.NET Framework中的Process类。下面我分别用这两种方法执行Windows中的记事本程序notepad.exe。
新建一个ASP.Net页面Default.aspx,在上面放一个按钮,进入Default.aspx.cs页面填写后台处理程序。
1、调用win32函数ShellExecute。
添加引用using System.Runtime.InteropServices;
声明函数:
[DllImport("shell32.dll")]
private static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, Int32 nShowCmd);
在按钮的单击事件处理程序中调用前面生明的ShellExecute函数:
ShellExecute(IntPtr.Zero, "open", "c:\\windows\\notepad.exe", null, null, 1);
//文件名可以不加后缀,如"c:\\windows\\notepad"。
程序完成2、用.NET Framework中的Process类
添加引用using System.Diagnostics;
在按钮的单击事件处理程序中填入以下代码:
Process process = new Process();
process.StartInfo.FileName = "c:\\windows\\notepad.exe";
//文件名必须加后缀。
process.Start();
程序完成。
注意:在asp.net2.0中运行以上两种方法写的程序都可以得到想要的结果。但是在asp.net1.1或更早的版本却不能得到想要的结果,打开任务管理器甚至可以看到进程已经存在(编译器也没有报错),但就是看不到执行效果,这是微软为了安全原因禁掉了程序的运行,你需要做以下两步才能正常运行:
1、在桌面右键单击“我的电脑”,选择管理,展开“服务和应用程序”节点,选择“服务”项;从右侧的服务列表中找到“IIS Admin”项,右击鼠标,选择“属性”,在属性框中打开“登陆”页,勾选“本地系统帐户”下的“允许与桌面交互”项,点击确定。重新启动这项服务。
2、打开目录“C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG”中的machine.config文件,找到“processModel”项,原来这项中有一个属性是userName="machine",将machine改为“system”,保存文件,修改完毕。
现在再运行以前的程序可以看到期望的结果。
以上程序在IIS5.0中可正常运行。
在asp.net中执行应用程序有两种方法:1、调用win32函数ShellExecute。2、用.NET Framework中的Process类。下面我分别用这两种方法执行Windows中的记事本程序notepad.exe。
新建一个ASP.Net页面Default.aspx,在上面放一个按钮,进入Default.aspx.cs页面填写后台处理程序。
1、调用win32函数ShellExecute。
添加引用using System.Runtime.InteropServices;
声明函数:
[DllImport("shell32.dll")]
private static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, Int32 nShowCmd);
在按钮的单击事件处理程序中调用前面生明的ShellExecute函数:
ShellExecute(IntPtr.Zero, "open", "c:\\windows\\notepad.exe", null, null, 1);
//文件名可以不加后缀,如"c:\\windows\\notepad"。
程序完成2、用.NET Framework中的Process类
添加引用using System.Diagnostics;
在按钮的单击事件处理程序中填入以下代码:
Process process = new Process();
process.StartInfo.FileName = "c:\\windows\\notepad.exe";
//文件名必须加后缀。
process.Start();
程序完成。
注意:在asp.net2.0中运行以上两种方法写的程序都可以得到想要的结果。但是在asp.net1.1或更早的版本却不能得到想要的结果,打开任务管理器甚至可以看到进程已经存在(编译器也没有报错),但就是看不到执行效果,这是微软为了安全原因禁掉了程序的运行,你需要做以下两步才能正常运行:
1、在桌面右键单击“我的电脑”,选择管理,展开“服务和应用程序”节点,选择“服务”项;从右侧的服务列表中找到“IIS Admin”项,右击鼠标,选择“属性”,在属性框中打开“登陆”页,勾选“本地系统帐户”下的“允许与桌面交互”项,点击确定。重新启动这项服务。
2、打开目录“C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG”中的machine.config文件,找到“processModel”项,原来这项中有一个属性是userName="machine",将machine改为“system”,保存文件,修改完毕。
现在再运行以前的程序可以看到期望的结果。
以上程序在IIS5.0中可正常运行。