System.Diagnostics.Process无法正常工作!!请帮忙 [英] System.Diagnostics.Process not working !! please help

查看:93
本文介绍了System.Diagnostics.Process无法正常工作!!请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我发布问题的第三个新闻组。

对不起多个帖子我很抱歉但问题变得紧急。


我希望这是适合它的地方...


我创建了一个非常简单的批处理文件(echo hello world)并试图

检索标准输出,但每次运行代码时都返回

ExitCode为1.


批处理文件从命令行运行得很好。 />

没有Win32Exception(没有ERROR_FILE_NOT_FOUND而且没有

ERROR_ACCESS_DENIED)


strOutput和strError也返回空。 />

ExitCode1是什么意思?


我错过了什么?


这是代码:


尝试

{

System.Diagnostics.Process MFGProc = new System.Diagnostics.Process();

MFGProc.StartInfo.FileName = strMfgWorkDir + strMfgBatFile;

MFGProc.StartInfo.WorkingDirectory = strMfgWorkDir;

MFGProc.StartInfo.UseShellExecute = false;

MFGProc.StartInfo.RedirectStandardError = true;

MFGProc.StartInfo.RedirectStandardOutput = true;

MFGProc.Start();


string strOutput = MFGProc.StandardOutput.ReadToEnd();

string strError = MFGProc。 StandardError.ReadToEnd();

MFGProc.WaitForExit();


if(MFGProc.ExitCode == 0)

{

Ok = true;

}

else

{

strErrorMsg =" ;运行失败。说明: + strError;

}

}

catch(Win32Exception e)

{

strErrorMsg =" W32错误:" + e.NativeErrorCode.ToString()+"。" +

e.Message;

}


catch(异常错误)

{

//在此处理其他错误

strErrorMsg =(" RunMFGCimSession其他错误.ErrorMessage:" +

err.Message);

}


谢谢


Nurit


This is the third newsgroup that I''m posting my problem.
I''m sorry for the multiple posts but the matter becoming urgent.

I hope this is the right place for it...

I have created a very simple batch file (echo hello world) and was trying to
retrieve the standard output but every time I run the code it returns
ExitCode as 1.

The batch file runs just fine from command line.

There is no Win32Exception (no ERROR_FILE_NOT_FOUND and no
ERROR_ACCESS_DENIED)

Also strOutput and strError returns empty.

What ExitCode1 means?

Am I missing on something?

Here is the code:

try
{
System.Diagnostics.Process MFGProc = new System.Diagnostics.Process();
MFGProc.StartInfo.FileName = strMfgWorkDir + strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory = strMfgWorkDir;
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start();

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if (MFGProc.ExitCode == 0)
{
Ok = true;
}
else
{
strErrorMsg = " Running failed. Description: " + strError;
}
}
catch (Win32Exception e)
{
strErrorMsg = "W32 Error:" + e.NativeErrorCode.ToString() + "." +
e.Message ;
}

catch (Exception err)
{
// Process other errors here
strErrorMsg = ("RunMFGCimSession Other error. ErrorMessage: " +
err.Message );
}

Thanks

Nurit


推荐答案



我刚尝试过你的代码,它对我来说很好(对于lil修改),

strOutput得到正确的输出。我在这里粘贴代码你检查

它对你有用。制作一个新的控制台应用程序并通过代码


// --------------< code> ;

使用System;


使用System.Diagnostics;

使用System.ComponentModel;


命名空间ConsoleApplication1

{


class csomething

{

public static void Main ()

{

csomething c = new csomething();

c.dosomething();


}

public void dosomething()

{


试试

{

System.Diagnostics.Process MFGProc = new System.Diagnostics.Process();

MFGProc.StartInfo.FileName =

AppDomain.CurrentDomain.BaseDirectory + @ " bat.bat英寸; // strMfgWorkDir +

strMfgBatFile;

MFGProc.StartInfo.WorkingDirectory =

AppDomain.CurrentDomain.BaseDirectory;

// @" D:\ mycode \ConsoleApplication1 \ConsoleApplication 1 \bin";

MFGProc.StartInfo.UseShellExecute = false;

MFGProc.StartInfo。 RedirectStandardError = true;

MFGProc.StartInfo.RedirectStandardOutput = true;

MFGProc.Start();


string strOutput = MFGProc .StandardOutput.ReadToEnd();

string strError = MFGProc.StandardError.ReadToEnd();

MFGProc.WaitForExit();


if(MFGProc.ExitCode == 0)

{

// Ok = true;

Console.Write(没问题 ;);


}

其他

{

// strErrorMsg ="运行失败。说明: + strError;

Console.Write(有些问题);

}

}

catch( Win32Exception e)

{

Console.Write(" W32 Error:" + e.NativeErrorCode.ToString()+"。" +

e.Message);

}


catch(异常错误)

{

//在此处理其他错误

Console.Write(" RunMFGCimSession其他错误.ErrorMessage:" +

err.Message);

}


}


} //班级


}
< br $>
// --------------< / code>


在您的调试bin目录中创建一个批处理文件将它命名为bat.bat并使用

notepad添加echo hello world在它。


-Abubakar。


++ ==

PS:为其他人粘贴代码时运行,确保它以完全的方式完成,任何人都可以在编辑器中复制并通过它,它会给你写的输出

。您的代码缺少变量并且使用语句缺少
。改变这些东西和机会需要时间

为你的帮助减少。因此,请务必提交完整的工作代码。

Plz阅读此链接 http://www.pobox.com/~skeet/csharp/complete.html

-Thanks


" ; Nurit N" <女**** @ sitqad.co.il>在消息中写道

news:eh ************* @ TK2MSFTNGP12.phx.gbl ...
Hi,
I just tried your code and it works fine ( with a lil modification) for me,
the strOutput gets the right output. I''m pasting the code here and you check
it works for you or not. Make a new console application and past the code
there:

//--------------<code>
using System;

using System.Diagnostics;
using System.ComponentModel;

namespace ConsoleApplication1
{

class csomething
{
public static void Main()
{
csomething c = new csomething();
c.dosomething();

}
public void dosomething()
{

try
{
System.Diagnostics.Process MFGProc = new System.Diagnostics.Process();
MFGProc.StartInfo.FileName =
AppDomain.CurrentDomain.BaseDirectory+@"bat.bat"; //strMfgWorkDir +
strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory =
AppDomain.CurrentDomain.BaseDirectory;
//@"D:\mycode\ConsoleApplication1\ConsoleApplication 1\bin";
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start();

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if (MFGProc.ExitCode == 0)
{
//Ok = true;
Console.Write("no problem");

}
else
{
//strErrorMsg = " Running failed. Description: " + strError;
Console.Write("some problem");
}
}
catch (Win32Exception e)
{
Console.Write("W32 Error:" + e.NativeErrorCode.ToString() + "." +
e.Message);
}

catch (Exception err)
{
// Process other errors here
Console.Write("RunMFGCimSession Other error. ErrorMessage: " +
err.Message);
}

}

}//class

}

//--------------</code>

make a batch file in your debug bin directory and name it bat.bat and use
notepad to add the "echo hello world" in it.

-Abubakar.

++==
P.S: when pasting your code for others to run, make sure its complete in a
way that anyone can just copy and past it in the editor and it would give
the output that your writing about. Your code had missing variables and
missing using statements. It takes time to change those things and chances
for your getting help reduces. So always submit the complete working code.
Plz read this link http://www.pobox.com/~skeet/csharp/complete.html
-Thanks

"Nurit N" <Nu****@sitqad.co.il> wrote in message
news:eh*************@TK2MSFTNGP12.phx.gbl...
这是第三个新闻组我正在发布我的问题。
我很抱歉这个帖子很多,但事情变得紧急。

我希望这是适合它的地方...

我创建了一个非常简单的批处理文件(echo hello world)并尝试
来检索标准输出,但每次运行代码时它都会返回
ExitCode为1.

批处理文件从命令行运行得很好。

没有Win32Exception(没有ERROR_FILE_NOT_FOUND而且没有
ERROR_ACCESS_DENIED)

strOutput和strError也是如此返回空。

ExitCode1是什么意思?

我错过了什么?

这是代码:

尝试
{
System.Diagnostics.Process MFGProc = new System.Diagnostics.Process();
MFGProc.StartInfo.FileName = strMfgWorkDir + strMfgBatFile;
MFGProc.StartInf o.WorkingDirectory = strMfgWorkDir;
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start( );

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if(MFGProc.ExitCode == 0)
{
Ok = true;
}



strErrorMsg ="运行失败。说明: + strError;
}
}
catch(Win32Exception e)
{
strErrorMsg =" W32错误:" + e.NativeErrorCode.ToString()+"。" +
e.Message;
}

catch(例外错误)
{
//在此处理其他错误
strErrorMsg =(" ; RunMFGCimSession其他错误.ErrorMessage:" +
err.Message);
}

Nurit

This is the third newsgroup that I''m posting my problem.
I''m sorry for the multiple posts but the matter becoming urgent.

I hope this is the right place for it...

I have created a very simple batch file (echo hello world) and was trying to retrieve the standard output but every time I run the code it returns
ExitCode as 1.

The batch file runs just fine from command line.

There is no Win32Exception (no ERROR_FILE_NOT_FOUND and no
ERROR_ACCESS_DENIED)

Also strOutput and strError returns empty.

What ExitCode1 means?

Am I missing on something?

Here is the code:

try
{
System.Diagnostics.Process MFGProc = new System.Diagnostics.Process();
MFGProc.StartInfo.FileName = strMfgWorkDir + strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory = strMfgWorkDir;
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start();

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if (MFGProc.ExitCode == 0)
{
Ok = true;
}
else
{
strErrorMsg = " Running failed. Description: " + strError;
}
}
catch (Win32Exception e)
{
strErrorMsg = "W32 Error:" + e.NativeErrorCode.ToString() + "." +
e.Message ;
}

catch (Exception err)
{
// Process other errors here
strErrorMsg = ("RunMFGCimSession Other error. ErrorMessage: " +
err.Message );
}

Thanks

Nurit



感谢您的重播。


我尝试了你发给我的代码,它也适用于我。


我在网络服务应用程序上运行

的不同(我很抱歉,之前没有提到过)。


我的程序假设在服务器上启动CMD。


任何想法?

" Abubakar" < AB ******* @ gmail.com>在消息中写道

news:uE ************** @ TK2MSFTNGP11.phx.gbl ...
Thanks for your replay.

I tried the code you sent me and it''s working for me as well.

The different (and I''m sorry not to mentioned that before) that I''m running
on a web service application.

My program supposes to start the CMD on the server.

Any Idea?
"Abubakar" <ab*******@gmail.com> wrote in message
news:uE**************@TK2MSFTNGP11.phx.gbl...
我刚尝试了你的代码,它可以很好地工作(通过lil修改)
我,
strOutput获得正确的输出。我在这里粘贴代码,你
检查
它是否适合你。创建一个新的控制台应用程序并通过代码
使用System;

// --------------< code>


使用System.Diagnostics;
使用System.ComponentModel;

命名空间ConsoleApplication1
{

类csomething {
public static void Main()
{
csomething c = new csomething();
c.dosomething();

}
public void dosomething()
尝试
{
System.Diagnostics.Process MFGProc = new System.Diagnostics.Process();
MFGProc.StartInfo.FileName =
AppDomain.CurrentDomain.BaseDirectory + @" bat.bat" ;; // strMfgWorkDir +
strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory =
AppDomain.CurrentDomain.BaseDirectory;
// @" D:\ mycode \ConsoleApplication1 \ConsoleApplication 1 \bin" ;;
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start( );

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if(MFGProc.ExitCode == 0)
{
// Ok = true;
Console.Write(没问题);
}

{
// strErrorMsg ="运行失败。说明: + strError;
Console.Write(一些问题);
}
}
catch(Win32Exception e)
{
Console.Write( W32错误:" + e.NativeErrorCode.ToString()+"。" +
e.Message);
}

catch(异常错误)
{
//在此处理其他错误
Console.Write(RunMFGCimSession其他错误.ErrorMessage:" +
err.Message);
}

} //

}

// ------------- - < / code>

在您的调试bin目录中创建一个批处理文件,并将其命名为bat.bat并使用
notepad添加echo hello world。在它中。

-Abubakar。

++ ==
PS:粘贴你的代码以供其他人运行时,请确保它在
任何人都可以在编辑器中复制并通过它的方式,它会给你写的输出。您的代码缺少变量并且缺少使用语句。改变这些事情和机会需要时间来帮助你减少帮助。因此,请务必提交完整的工作代码。
Plz阅读此链接 http://www.pobox.com/~skeet/csharp/complete.html
-Thanks

Nurit N <女**** @ sitqad.co.il>在消息中写道
新闻:eh ************* @ TK2MSFTNGP12.phx.gbl ...
Hi,
I just tried your code and it works fine ( with a lil modification) for
me,
the strOutput gets the right output. I''m pasting the code here and you
check
it works for you or not. Make a new console application and past the code
there:

//--------------<code>
using System;

using System.Diagnostics;
using System.ComponentModel;

namespace ConsoleApplication1
{

class csomething
{
public static void Main()
{
csomething c = new csomething();
c.dosomething();

}
public void dosomething()
{

try
{
System.Diagnostics.Process MFGProc = new System.Diagnostics.Process();
MFGProc.StartInfo.FileName =
AppDomain.CurrentDomain.BaseDirectory+@"bat.bat"; //strMfgWorkDir +
strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory =
AppDomain.CurrentDomain.BaseDirectory;
//@"D:\mycode\ConsoleApplication1\ConsoleApplication 1\bin";
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start();

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if (MFGProc.ExitCode == 0)
{
//Ok = true;
Console.Write("no problem");

}
else
{
//strErrorMsg = " Running failed. Description: " + strError;
Console.Write("some problem");
}
}
catch (Win32Exception e)
{
Console.Write("W32 Error:" + e.NativeErrorCode.ToString() + "." +
e.Message);
}

catch (Exception err)
{
// Process other errors here
Console.Write("RunMFGCimSession Other error. ErrorMessage: " +
err.Message);
}

}

}//class

}

//--------------</code>

make a batch file in your debug bin directory and name it bat.bat and use
notepad to add the "echo hello world" in it.

-Abubakar.

++==
P.S: when pasting your code for others to run, make sure its complete in a
way that anyone can just copy and past it in the editor and it would give
the output that your writing about. Your code had missing variables and
missing using statements. It takes time to change those things and chances
for your getting help reduces. So always submit the complete working code.
Plz read this link http://www.pobox.com/~skeet/csharp/complete.html
-Thanks

"Nurit N" <Nu****@sitqad.co.il> wrote in message
news:eh*************@TK2MSFTNGP12.phx.gbl...
这是我的第三个新闻组发布我的问题。
我很抱歉这个帖子很多,但事情变得紧急。

我希望这是适合它的地方...
我创建了一个非常简单的批处理文件(echo hello world),并尝试
This is the third newsgroup that I''m posting my problem.
I''m sorry for the multiple posts but the matter becoming urgent.

I hope this is the right place for it...

I have created a very simple batch file (echo hello world) and was trying


检索标准输出,但每次运行代码时它返回
ExitCode为1.

批处理文件从命令行运行得很好。

没有Win32Exception(没有ERROR_FILE_NOT_FOUND而且没有
ERROR_ACCESS_DENIED)

strOutput和strError也是空的。

ExitCode1是什么意思?

我错过了什么?

这是代码:

尝试
{
System.Diagnostics.Process MFGProc = new
System.Diagnostics.Process();
MFGProc.StartInfo.FileName = strMfgWork Dir + strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory = strMfgWorkDir;
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start();

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if(MFGProc.ExitCode == 0)
{
Ok = true;
}


{
strErrorMsg ="运行失败。说明: + strError;
}
}
catch(Win32Exception e)
{
strErrorMsg =" W32错误:" + e.NativeErrorCode.ToString()+"。" +
e.Message;
}

catch(例外错误)
{
//在此处理其他错误
strErrorMsg =(" ; RunMFGCimSession其他错误.ErrorMessage:" +
err.Message);
}

Nurit

retrieve the standard output but every time I run the code it returns
ExitCode as 1.

The batch file runs just fine from command line.

There is no Win32Exception (no ERROR_FILE_NOT_FOUND and no
ERROR_ACCESS_DENIED)

Also strOutput and strError returns empty.

What ExitCode1 means?

Am I missing on something?

Here is the code:

try
{
System.Diagnostics.Process MFGProc = new
System.Diagnostics.Process();
MFGProc.StartInfo.FileName = strMfgWorkDir + strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory = strMfgWorkDir;
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start();

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if (MFGProc.ExitCode == 0)
{
Ok = true;
}
else
{
strErrorMsg = " Running failed. Description: " + strError;
}
}
catch (Win32Exception e)
{
strErrorMsg = "W32 Error:" + e.NativeErrorCode.ToString() + "." +
e.Message ;
}

catch (Exception err)
{
// Process other errors here
strErrorMsg = ("RunMFGCimSession Other error. ErrorMessage: " +
err.Message );
}

Thanks

Nurit






您必须有权限问题。您需要为您尝试从虚拟目录上的硬盘驱动器访问的目录提供适当的

权限。您可以右键单击

批处理文件所在的目录,然后转到安全部分。选项卡,在其中添加ASPNET用户和

给予适当的权限。虽然这是不安全的,但我会相信,但是首先要尝试这个。


我在visual studio 2k5中创建了一个新的web应用程序,但是有

运行此代码没有问题。你运行这个

代码时会得到什么确切的错误?


Ab。


Nurit N ; <女**** @ sitqad.co.il>在消息中写道

news:uY ************** @ tk2msftngp13.phx.gbl ...

You must be having permissions problem. You need to give approriate
permissions to the directory that you are trying to access on the harddrive
from the virtual directory. You can right click the directory where your
batch file is and goto the "security" tab, add the ASPNET user in it and
give appropriate permissions. Although this would be insecure i beleive but
first try this.

I created a new web appplication to do this in visual studio 2k5, but have
no problems running this code. What exact error do u get when you run this
code?

Ab.

"Nurit N" <Nu****@sitqad.co.il> wrote in message
news:uY**************@tk2msftngp13.phx.gbl...
感谢您的重播。

我尝试了你发给我的代码,它也适用于我。

不同的(我很遗憾以前没有提到)我是
在web服务应用程序上运行。

我的程序假设在服务器上启动CMD。

任何想法?

阿布巴卡尔 < AB ******* @ gmail.com>在消息中写道
新闻:uE ************** @ TK2MSFTNGP11.phx.gbl ...
Thanks for your replay.

I tried the code you sent me and it''s working for me as well.

The different (and I''m sorry not to mentioned that before) that I''m running on a web service application.

My program supposes to start the CMD on the server.

Any Idea?
"Abubakar" <ab*******@gmail.com> wrote in message
news:uE**************@TK2MSFTNGP11.phx.gbl...

我刚试过你的代码和它工作正常(通过lil修改)
我,
strOutput获得正确的输出。我在这里粘贴代码,你
检查
它是否适合你。制作一个新的控制台应用程序,并在那里使用
代码:

// --------------< code>
使用System;

使用System.Diagnostics;
使用System.ComponentModel;

命名空间ConsoleApplication1
{

类csomething {
public static void Main()
{
csomething c = new csomething();
c.dosomething();

}
public void dosomething()
尝试
{/> System.Diagnostics.Process MFGProc = new
System.Diagnostics.Process(); MFGProc.StartInfo.FileName =
AppDomain.CurrentDomain.BaseDirectory + @" bat.bat" ;; // strMfgWorkDir +
strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory =
AppDomain.CurrentDomain.BaseDirectory;
// @" D:\ mycode \ConsoleApplication1 \ConsoleApplication 1 \bin" ;;
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start( );

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if(MFGProc.ExitCode == 0)
{
// Ok = true;
Console.Write(没问题);
}

{
// strErrorMsg ="运行失败。说明: + strError;
Console.Write(一些问题);
}
}
catch(Win32Exception e)
{
Console.Write( W32错误:" + e.NativeErrorCode.ToString()+"。" +
e.Message);
}

catch(异常错误)
{
//在此处理其他错误
Console.Write(RunMFGCimSession其他错误.ErrorMessage:" +
err.Message);
}

} //

}

// ------------- - < / code>

在您的调试bin目录中创建一个批处理文件,并将其命名为bat.bat,
使用notepad添加echo hello world在它中。

-Abubakar。

++ ==
PS:粘贴你的代码以供其他人运行时,请确保它在
a中完成任何人都可以在编辑器中复制并通过它的方式,它将
给出你写的输出。您的代码缺少变量并且缺少使用语句。改变这些东西需要时间,并且你获得帮助的机会减少了b $ b。所以总是提交完整的工作
代码。 Plz阅读此链接 http://www.pobox.com/~ skeet / csharp / complete.html
-Thanks

Nurit N <女**** @ sitqad.co.il>在消息中写道
新闻:eh ************* @ TK2MSFTNGP12.phx.gbl ...
Hi,
I just tried your code and it works fine ( with a lil modification) for
me,
the strOutput gets the right output. I''m pasting the code here and you
check
it works for you or not. Make a new console application and past the code there:

//--------------<code>
using System;

using System.Diagnostics;
using System.ComponentModel;

namespace ConsoleApplication1
{

class csomething
{
public static void Main()
{
csomething c = new csomething();
c.dosomething();

}
public void dosomething()
{

try
{
System.Diagnostics.Process MFGProc = new System.Diagnostics.Process(); MFGProc.StartInfo.FileName =
AppDomain.CurrentDomain.BaseDirectory+@"bat.bat"; //strMfgWorkDir +
strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory =
AppDomain.CurrentDomain.BaseDirectory;
//@"D:\mycode\ConsoleApplication1\ConsoleApplication 1\bin";
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start();

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if (MFGProc.ExitCode == 0)
{
//Ok = true;
Console.Write("no problem");

}
else
{
//strErrorMsg = " Running failed. Description: " + strError;
Console.Write("some problem");
}
}
catch (Win32Exception e)
{
Console.Write("W32 Error:" + e.NativeErrorCode.ToString() + "." +
e.Message);
}

catch (Exception err)
{
// Process other errors here
Console.Write("RunMFGCimSession Other error. ErrorMessage: " +
err.Message);
}

}

}//class

}

//--------------</code>

make a batch file in your debug bin directory and name it bat.bat and use notepad to add the "echo hello world" in it.

-Abubakar.

++==
P.S: when pasting your code for others to run, make sure its complete in a way that anyone can just copy and past it in the editor and it would give the output that your writing about. Your code had missing variables and
missing using statements. It takes time to change those things and chances for your getting help reduces. So always submit the complete working code. Plz read this link http://www.pobox.com/~skeet/csharp/complete.html
-Thanks

"Nurit N" <Nu****@sitqad.co.il> wrote in message
news:eh*************@TK2MSFTNGP12.phx.gbl...
这是我的第三个新闻组发布我的问题。
我很抱歉这个帖子很多,但事情变得紧急。

我希望这是适合它的地方...
我创建了一个非常简单的批处理文件(echo hello world)并且
This is the third newsgroup that I''m posting my problem.
I''m sorry for the multiple posts but the matter becoming urgent.

I hope this is the right place for it...

I have created a very simple batch file (echo hello world) and was


尝试

检索标准输出,但每次运行代码时它返回
ExitCode为1.

批处理文件从命令行运行得很好。

没有Win32Exception(没有ERROR_FILE_NOT_FOUND而且没有
ERROR_ACCESS_DENIED)

strOutput和strError也是空的。

ExitCode1是什么意思?

我错过了什么?

这是代码:

尝试
{
System.Diagnostics.Process MFGProc = new
System.Diagnostics.Process();
MFGProc.StartInfo.FileName = strMfgWorkD ir + strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory = strMfgWorkDir;
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start();

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if(MFGProc.ExitCode == 0)
{
Ok = true;
}


{
strErrorMsg ="运行失败。说明: + strError;
}
}
catch(Win32Exception e)
{
strErrorMsg =" W32错误:" + e.NativeErrorCode.ToString()+"。" +
e.Message;
}

catch(例外错误)
{
//在此处理其他错误
strErrorMsg =(" ; RunMFGCimSession其他错误.ErrorMessage:" +
err.Message);
}

Nurit

retrieve the standard output but every time I run the code it returns
ExitCode as 1.

The batch file runs just fine from command line.

There is no Win32Exception (no ERROR_FILE_NOT_FOUND and no
ERROR_ACCESS_DENIED)

Also strOutput and strError returns empty.

What ExitCode1 means?

Am I missing on something?

Here is the code:

try
{
System.Diagnostics.Process MFGProc = new
System.Diagnostics.Process();
MFGProc.StartInfo.FileName = strMfgWorkDir + strMfgBatFile;
MFGProc.StartInfo.WorkingDirectory = strMfgWorkDir;
MFGProc.StartInfo.UseShellExecute = false;
MFGProc.StartInfo.RedirectStandardError = true;
MFGProc.StartInfo.RedirectStandardOutput = true;
MFGProc.Start();

string strOutput = MFGProc.StandardOutput.ReadToEnd();
string strError = MFGProc.StandardError.ReadToEnd();
MFGProc.WaitForExit();

if (MFGProc.ExitCode == 0)
{
Ok = true;
}
else
{
strErrorMsg = " Running failed. Description: " + strError;
}
}
catch (Win32Exception e)
{
strErrorMsg = "W32 Error:" + e.NativeErrorCode.ToString() + "." +
e.Message ;
}

catch (Exception err)
{
// Process other errors here
strErrorMsg = ("RunMFGCimSession Other error. ErrorMessage: " +
err.Message );
}

Thanks

Nurit





这篇关于System.Diagnostics.Process无法正常工作!!请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆