如何检测哪个程序返回了错误代码?

发布于 2024-09-08 12:31:53 字数 2103 浏览 16 评论 0原文

我尝试以编程方式运行一系列命令,读取错误代码,并检测这些代码是否指示成功或失败,以便我可以做出相应的响应。

目前我的命令使用 psexec,然后启动 robocopy。我注意到,虽然如果程序成功,大多数命令都会返回错误代码 0,但 robocopy 很奇怪,即使操作成功,它也会返回 0-8 范围内的值,因此我在其中添加了一些额外的逻辑当 robocopy 返回错误代码时,我会进行错误检测,否则表明失败。

问题是,在同一组命令中,我使用 PSExec 启动各种其他可执行文件和批处理文件,因此我需要一个错误检测解决方案,使我能够知道 robocopy 何时返回这些错误代码,或者是否是 PSExec,因为robocopy 中的错误代码 5 通常没有问题,而 PSExec 中的错误代码 5 表示访问被拒绝。

所以我的问题是,我如何知道哪个程序返回了错误代码?我使用的是 c# .NET 4.0,并且使用 Process 类以编程方式启动这些程序。我将程序名称设置为 psexec,参数包括 robocopy 或其他程序。然后我运行,等待退出,并存储错误代码,然后尝试解析它。

大家有什么建议呢?

这是一个代码片段:

foreach (var command in commands)
        {
            // TODO: Add exception handling
            string processName = command.Split(delimiters).ToList().ElementAt(0);    // split up command into pieces, select first "token" as the process name
            string commandArguments = command.Replace(processName + " ", ""); // remove the process name and following whitespace from the command itself, storing it in a new variable
            Process commandProcess = new Process(); // declare a new process to be used
            commandProcess.StartInfo.FileName = processName;    // add file start info for filename to process
            commandProcess.StartInfo.Arguments = commandArguments;  // add file start info for arguments to process
            commandProcess.StartInfo.UseShellExecute = false;  // skip permissions request
            commandProcess.Start();   // start process according to command's data
            commandProcess.WaitForExit();   // wait for the process to exit before continuing
            bool commandSuccessful = ParseCommandErrorCode(commandProcess, commandProcess.ExitCode);    // grab error code
            if (!commandSuccessful)
            {
                // ERROR! abort operation and inform the user of the last completed operation, and how many commands have not been run
            } // end if
            Console.WriteLine("Error code: {0}", commandProcess.ExitCode);    // print error code
            commandProcess.Close(); // close process
        } // end foreach

I'm attempting to run a series of commands programmatically, read the error codes, and detect if these codes indicate success or failure so I can respond accordingly.

Currently my commands are using psexec which then launches robocopy. I've noted that while most commands return an error code of 0 if the program is successful, robocopy is odd in that it returns values in the range of 0-8 even if the operation is successful, so I am adding some extra logic in my error detection to note when robocopy returns an error code which otherwise suggests a failure.

The problem is that in this same set of commands I'm using PSExec to launch various other executables and batch files, so I need an error detection solution that allows me to know when robocopy is the one returning these error codes or if it's PSExec because an error code of 5 in robocopy is fine usually whereas an error code of 5 in PSExec says that access is denied.

So my question is, how do I know which program has returned the error code? I'm using c# .NET 4.0, and I'm using the Process class to programmatically launch these programs. I set the program name as psexec, and the arguments include the robocopy or other programs. I then run, wait for the exit, and store the error code, then attempt to parse it.

What do you all suggest?

Here is a code snippet:

foreach (var command in commands)
        {
            // TODO: Add exception handling
            string processName = command.Split(delimiters).ToList().ElementAt(0);    // split up command into pieces, select first "token" as the process name
            string commandArguments = command.Replace(processName + " ", ""); // remove the process name and following whitespace from the command itself, storing it in a new variable
            Process commandProcess = new Process(); // declare a new process to be used
            commandProcess.StartInfo.FileName = processName;    // add file start info for filename to process
            commandProcess.StartInfo.Arguments = commandArguments;  // add file start info for arguments to process
            commandProcess.StartInfo.UseShellExecute = false;  // skip permissions request
            commandProcess.Start();   // start process according to command's data
            commandProcess.WaitForExit();   // wait for the process to exit before continuing
            bool commandSuccessful = ParseCommandErrorCode(commandProcess, commandProcess.ExitCode);    // grab error code
            if (!commandSuccessful)
            {
                // ERROR! abort operation and inform the user of the last completed operation, and how many commands have not been run
            } // end if
            Console.WriteLine("Error code: {0}", commandProcess.ExitCode);    // print error code
            commandProcess.Close(); // close process
        } // end foreach

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮光之海 2024-09-15 12:31:53

你不是已经回答了你自己的问题了吗?您必须将错误代码与每个实用程序的成功或失败进行匹配。 commandProcess.StartInfo.FileName 将告诉您启动的实用程序是 robocopy 还是 PSExe。解析错误代码时,根据文件名将值映射到成功或失败。

Haven't you already answered your own question? You will have to match the error code to success or failure for each utility. commandProcess.StartInfo.FileName will tell you if the utility launched was robocopy or PSExe. When parsing the error code, map the value to success or failure depending on the name of the file.

绝情姑娘 2024-09-15 12:31:53

我做大型数据库的备份,所以有一个命令文件.cmd。因为我有 mny Start ROBOCOPY 源目标和参数每个进程脚本行 a* b* c* c* 等。
示例如下:在sample.cmd文件中(充当btach文件),
我在sample.cmd 文件中有以下几行脚本:

Start ROBOCOY src dest a* b* c* /z /w:1 r:1

Start ROBOCOY src dest d* e* f* g* /z /w: 1 r:1

启动 ROBOCOY src dest h* K* P* Y* /z /w:1 r:1

启动 ROBOCOY src dest xry* srp* /z /w:1 r:1

当我朗姆酒 > 时Robocopy Sample.cmd 我从 cmd.exe 和另外 4 个多个控制台窗口 robocoy.exe 开始,按照上述命令同时复制文件,它等待另一个文件,因为它有等待时间,如果文件正在被另一个进程使用。因为它同时工作,所以速度更快。

现在我正在使用 C# windows 开发 GUI 来运行该进程,而不是转到命令控制台和
start

main()

{

process.start( "path of sample.cmd" )

process.waitforexit()

label.text=" successful copy"

}

但是,如果它控制了一个进程,即 cmd.exe 并且有 4 个 robocopy任务管理器中的进程。当 cmd.exe 进程完成时,它将光标返回到 label.text“成功完成”。虽然 robocopy 进程仍在运行。您可以看到 robocopy 窗口正在执行复制过程。

问题是:我想在 C# 中以编程方式控制所有进程(cmd.exe 和 robocopy.exe),以便只有当所有命令都成功完成时,label.text 才应显示“成功完成”,如果一个失败了,那么 GUI 中就没有任何意义了

(类似于 Biju 上面写的):最好从 example.cmd(批处理文件)文件中删除 robocoy 命令脚本并编写代码来运行 4 robocopy 行C#,但如何运行 robocooy 脚本行编写的 .cmd 文件,因为它们也有参数,我代码运行每个 robocopy 进程,然后每个进程将返回到下一行代码,如果失败,我们可以捕获错误并显示。 希望这会有所

帮助...但是,我正在寻找更好的方法,如果有人可以改进的话,谢谢:)

I do backup of large database, so there is a command file .cmd. in that I have mny Start ROBOCOPY cource destination and arguments each process script line for a* b* c* c* etc.
example as below: in sample.cmd file(acts as btach file),
I have following lines of scripts in a sample.cmd file as:

Start ROBOCOY src dest a* b* c* /z /w:1 r:1

Start ROBOCOY src dest d* e* f* g* /z /w:1 r:1

Start ROBOCOY src dest h* K* P* Y* /z /w:1 r:1

Start ROBOCOY src dest xry* srp* /z /w:1 r:1

When I rum > Robocopy sample.cmd I starts with cmd.exe and another 4 multiple console windows robocoy.exe copying files simultaneously as per above commands, it waits for another file, as it has wait time, if file is being used by another process. Is is more faster as it do job simultaneously.

Now I am devloping GUI using C# windows to run the process instead going to command console and
start

main()

{

process.start( "path of sample.cmd" )

process.waitforexit()

label.text=" sucessful copy"

}

However, if it takes control of oneprocess, i.e cmd.exe and and there are 4 robocopy processes in taskmanager. when cmd.exe process completes, it returns the cursor to label.text "Sucesssfully completed". While there are robocopy processes still running. you can see the robocopy windows doing the copying process.

Here is the question: I want to take contrl of all the processes (cmd.exe and robocopy.exe) programatically in C#, so that when the label.text should display "sucessfully completed" only when all commands are successfully completed", if one fails, then there is no point in the GUI.

option 2 (similar to Biju has written above): is it better to remove robocoy command scripts from sample.cmd(batch file) file and write code to run the 4 robocopy lines in C#, but how to run the robocooy script line written .cmd file, as they have arguments as well. I code runs each robocopy process then each will return to the next line of code and if it fails, we can catch the error and display in the message box.

Hope this will help... However, Iam looking for more better way, if somebody can improve on the same. Thanks :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文