我尝试使用osql进行登录。我知道在从cmd运行时会抛出异常,但在下面的代码中我没有捕捉到异常。如何捕获异常?
p.StartInfo.FileName = "osql.exe";
p.StartInfo.Arguments = "-S serverName -U userName -P \"psw\" -d db";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
try
{
p.Start();
// should not get here
}
catch(Exception ex)
{
// should get here
}发布于 2015-05-13 16:10:09
如果在osql.exe上登录失败,System.Diagnostic.Process 'p‘不会引发异常。也许osql.exe的结果会返回一个win32错误代码...不能使用try catch块处理来自应用程序中其他进程的loginexception
编辑:
您可以通过P.ExitCode接收ExitCode。考虑使用WaitForExit()方法等待osql.exe进程退出。
p.Start();
p.WaitForExit(); //Wait for the Application to exit
if (p.ExitCode <> 0) //If <>0 the operation failed
{ ///Operation failed
}在我的机器上,结果总是0或1-我认为你不会得到登录错误的确切错误代码。只是一个失败或成功的信息。
https://stackoverflow.com/questions/30208943
复制相似问题