Click here to Skip to main content
15,887,966 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Program runs a process whose definition exists in different class. I recently added background worker to display the status of the work in progress when a cmd process is running.

the code looks somewhat like this in the presenter
C#
string command = @"xcopy C:\test1.txt C:\test2.txt";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = command;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
if(process.ExitCode == 0)
return true;
else
return false;



The problem here is the process returns true despite the file not being copied.

When I use the same code without any background worker it works fine, however using it doesn't do the work.

Any help will be appreciated.

What I have tried:

Also tried

C#
string command = @"xcopy C:\test1.txt C:\test2.txt";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine(command);
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
if(process.ExitCode == 0)
return true;
else
return false;
Posted
Updated 9-Dec-16 2:43am

Why on earth are you using xcopy to copy a file?
Just use File.Copy instead of kicking off a separate process, and you can catch any exception and return your status code as a result.

If you must do this, then you need to catch exceptions - if the xcopy fails, the process will throw an exception (such as "file not found" will give a System.ComponentModel.Win32Exception) and the background worker thread will be terminated if you don't handle it. It's quite likely that this is happening here.
 
Share this answer
 
Try something like this:

C#
StreamReader outputReader = null;
StreamReader errorReader = null;
bool success = false;
using (Process process = new Process()
        {
            StartInfo =  new ProcessStartInfo()
            {
                RedirectStandardError = true,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                FileName = "xcopy",
                Arguments = @"SourceFile c:\DestinationFile /e /y /I"
            }
        })
{
    try
    {
        bool processStarted = process.Start();
        if (processStarted)
        {
            //Get the output stream
            outputReader = process.StandardOutput;
            errorReader = process.StandardError;
            process.WaitForExit();
            success = (process.ExitCode == 0);

            //Display the result
            string displayText = string.Concat("Output", Environment.NewLine, "==============", Environment.NewLine);
            displayText = string.Concat(displayText, outputReader.ReadToEnd());
            displayText = string.Concat(displayText, Environment.NewLine, "Error", Environment.NewLine, "==============", Environment.NewLine;
            displayText += errorReader.ReadToEnd();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        if (outputReader != null)
        {
            outputReader.Close();
        }
        if (errorReader != null)
        {
            errorReader.Close();
        }
    }
}
if (success)
{
    // ... do something)
}
else
{
    // ... do something else
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900