Process类在webform中可以运行,但是在winform中却不可以运行

bbb332 2012-07-27 10:55:53
webform中的代码
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string filename = FileUpload1.FileName;
//string extension=Path.GetExtension(filename); //获取后缀名
string file_name = filename.Substring(0, filename.LastIndexOf("."));//获取不含扩展名的文件名
string file_storage = Server.MapPath("~/File/storage/") + filename; //原文件存放的路径
FileUpload1.SaveAs(file_storage);
string file_bowze = Server.MapPath("~/File/bowze/") + file_name + ".swf"; //转换为SWF格式后存放的路径
string flash_file = Server.MapPath("~/FlashPaper2.2/FlashPrinter.exe");
Process pro = new Process();
pro.StartInfo.FileName = "cmd"; //调用CMD线程
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = true; //注册输入指向
pro.StartInfo.RedirectStandardOutput = true; //注册输出指向
pro.StartInfo.CreateNoWindow = true; //不建新窗口,这个属性可以根据需要自己设定
pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //隐藏窗口
pro.Start();
string input = string.Format("{0} {1} -o {2}", flash_file, file_storage, file_bowze); //类似于在DOS界面要输入的内容
pro.StandardInput.WriteLine(input); //将内容写到DOS ---个人是这么理解的
pro.StandardInput.WriteLine("exit");
pro.WaitForExit(600000);
string output = pro.StandardOutput.ReadToEnd(); //这句一定要加,输出转换好的SWF格式文件到指定位置 ,我一开始没加怎么都得不到文件
pro.Close(); //关掉线程
}
}


这个在webform中是可以运行的
在winfrom中的代码如下,但是不可以运行

private void button2_Click(object sender, EventArgs e)
{
string flash_file = Application.StartupPath + "\\FlashPaper2.2\\FlashPrinter.exe";
string file_storage = Application.StartupPath + "\\2.doc";
string file_bowze = Application.StartupPath + "\\2.swf";
Process pro = new Process();
pro.StartInfo.FileName = "cmd"; //调用CMD线程
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = true; //注册输入指向
pro.StartInfo.RedirectStandardOutput = true; //注册输出指向
pro.StartInfo.CreateNoWindow = true; //不建新窗口,这个属性可以根据需要自己设定
pro.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; //隐藏窗口
try
{
pro.Start();
string input = string.Format("{0} {1} -o {2}", flash_file, file_storage, file_bowze); //类似于在DOS界面要输入的内容
pro.StandardInput.WriteLine(input); //将内容写到DOS ---个人是这么理解的
pro.StandardInput.WriteLine("exit");
pro.WaitForExit(600000);
string output = pro.StandardOutput.ReadToEnd(); //这句一定要加,输出转换好的SWF格式文件到指定位置 ,我一开始没加怎么都得不到文件
pro.Close(); //关掉线程
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}


请教一下了,谢谢了
...全文
162 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qldsrx 2012-07-27
  • 打赏
  • 举报
回复
哪行代码报错?错误信息是什么?我早在WinForm里用过Process类,没任何问题,你不说明怎么个不可用,谁知道错在哪里?
timfeng2009 2012-07-27
  • 打赏
  • 举报
回复
我的代码如下,在控制台应用程序中运行没有问题。

string filePath = filename;//需要转换的文件路径
string swfPath = filePath.Substring(0, filePath.LastIndexOf('.')) + ".swf";
if (!File.Exists(filePath) || File.Exists(swfPath)) { return; }

FileInfo finfo = new FileInfo(filePath);
int time = ((int)(finfo.Length / (1024 * 1204))) * 20 + 20;
Process pc = new System.Diagnostics.Process();
pc.StartInfo.FileName = "flashprinter";//需要启动的程序路径
pc.StartInfo.Arguments = string.Format("{0} -o {1}", filePath, swfPath);
pc.StartInfo.CreateNoWindow = true;
pc.StartInfo.UseShellExecute = false;
pc.StartInfo.RedirectStandardInput = false;
pc.StartInfo.RedirectStandardOutput = false;
pc.StartInfo.RedirectStandardError = true;
pc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pc.Start();
bool res = pc.WaitForExit(time * 1000);

pc.Close();
pc.Dispose();
孟子E章 2012-07-27
  • 打赏
  • 举报
回复
另外不要在系统盘上测试。
qldsrx 2012-07-27
  • 打赏
  • 举报
回复
不报错说明程序没错,而你CMD命令中执行另一个命令,那个命令的执行情况你并未捕获,你先将隐藏DOS窗口的代码去掉,并将DOS窗口“exit”退出的代码也去掉,手动关闭,这样才能进行调试DOS内部的执行情况。
isjoe 2012-07-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
在server2003中是可以的,但是在win7下是不行的,不知道为什么,求解。。。谢谢了
[/Quote]

会不会是权限问题呢,在win7下,以管理员Administrator登录,试一下
孟子E章 2012-07-27
  • 打赏
  • 举报
回复
先自己在命令行模式下测试
孟子E章 2012-07-27
  • 打赏
  • 举报
回复
请检查您每一步的完整路径,尤其是
string flash_file = Application.StartupPath + "\\FlashPaper2.2\\FlashPrinter.exe";
string file_storage = Application.StartupPath + "\\2.doc";
string file_bowze = Application.StartupPath + "\\2.swf";

你打印出看什么,真的存在这些路径吗?路径带空格吗?带空格要加引号
bbb332 2012-07-27
  • 打赏
  • 举报
回复
在server2003中是可以的,但是在win7下是不行的,不知道为什么,求解。。。谢谢了
bbb332 2012-07-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
我的代码如下,在控制台应用程序中运行没有问题。

C# code


string filePath = filename;//需要转换的文件路径
string swfPath = filePath.Substring(0, filePath.LastIndexOf('.')) + ".swf";
if (!Fi……
[/Quote]

不报错,直接运行完毕。。。。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧