XCOPY return ExitCode 4 in C# code.

Z

Zeya

I am developing a ASP.Net(Web) based deployment tool, which requires
XCOPY functionality.

Here is the code for the method:

public static int ExecuteProcess ( string ProcessName, string
ProcessArguments )
{
string ErrorStream = "";
Process DOSProcess = new Process();
ProcessStartInfo StartInfo = new ProcessStartInfo();

StartInfo.FileName = @"CMD.exe ";

StartInfo.RedirectStandardError = false;
StartInfo.RedirectStandardOutput = false;
StartInfo.RedirectStandardInput = false;

StartInfo.UseShellExecute = false;
StartInfo.CreateNoWindow = true;

StartInfo.Arguments = @"/D /c " + ProcessName + " " +
ProcessArguments;

DOSProcess.EnableRaisingEvents = true;
DOSProcess.StartInfo = StartInfo;
DOSProcess.Start();
DOSProcess.WaitForExit( 15000 );
int ProcessExitCode = DOSProcess.ExitCode ;

if ( ProcessExitCode > 0 & !DOSProcess.HasExited )
DOSProcess.Kill();

DOSProcess.Dispose();
StartInfo = null;
return ProcessExitCode;
}

ExecuteProcess method takes ProcessName (in my case: XCOPY) and
ProcessArguments (in my case: the arguments needed for the XCOPY)

I stepped through to check the arguments assigned, I found:
/D /c XCOPY "E:\MySourcedirectory\Source\BUILDNUMBER\WebRoot"
"E:\MyDestinationDirectory\Destination\web" /S /E /F /H /R /Y /i

These are the switches I am passing for XCOPY: /S /E /F /H /R /Y /i

This runs excellent when I directly run from Command line as: cmd.exe
/D /c XCOPY "E:\MySourcedirectory\Source\BUILDNUMBER\WebRoot"
"E:\MyDestinationDirectory\Destination\web" /S /E /F /H /R /Y /i

The program first calls this method to back up existing files and then
copies the new files to the location. Back up works well but deployment
it does not do anything and returns exit code: 4.

According to MSDN, ExitCode 4 means: Initialization error occurred.
There is not enough memory or disk space, or you entered an invalid
drive name or invalid syntax on the command line.

As I mentioned, this runs smoothly from commandline.

When I use StandardError.ReadToEnd() it returns empty string.

Where am I going wrong? Any suggestions?

Thanks in advance.
 
T

The Crow

i think this is a permission issue. asp.net is not a previlliged user. can
you try to run the same code in a console or windows application? it seem to
work ok. if so, you will give asp.net account required permissions.
 
Z

Zeya

Thank you Crow for your response.

The application uses Window authentication. And if I understand
correctly, the permissions will be same as the user.

Also, the same works in one case and returns ExitCode 4 in the other.
 
W

Willy Denoyette [MVP]

No it wont, the spawned CMD will use the identity of your asp worker
process, not the identity of the impersonated user.
 
Z

Zeya

Willy said:
No it wont, the spawned CMD will use the identity of your asp worker
process, not the identity of the impersonated user.


Thanks Willy. But I am not fully convinced. Reason being that I am able
to do a XCOPY in another subdirectory of the parent directory and it
works fine. (This happens when I am backing up the current files). If
the backup process could be executed with no issues then deploying is
the same process.

Would you have any other method that I could use to do this?
 
W

Willy Denoyette [MVP]

Zeya said:
Thanks Willy. But I am not fully convinced. Reason being that I am able
to do a XCOPY in another subdirectory of the parent directory and it
works fine. (This happens when I am backing up the current files). If
the backup process could be executed with no issues then deploying is
the same process.

Would you have any other method that I could use to do this?

If you are not convinced by what I'm saying (it's perfectly acceptable to be
sceptic ;-)) you can convince yourself by looking for the identity of the
CMD process in taskman when it runs your XCOPY.
This asside, when it works you are reading from the home directory and
writing into another subdir of the home dir. It's when you try to write into
the home dir that it fails, right? Well the you need to check the access
privileges and look if aspnet or whtever the identity of the CMD process may
be has writte permissions for that home directory.


Willy.
 
Z

Zeya

Willy,

I did not mean to be offensive by saying I am not convinced and I
absolutely appreciate your help.

However, I dropped the idea of using XCOPY instead I used Robocopy and
it worked like a charm.

So, again thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top