English 中文(简体)
How do I detect which program has returned an error code?
原标题:

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
最佳回答

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.

问题回答

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 :)





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签