I have a batch file like this
@echo off
xcopy /e %1 %2
I have my C# code as follows:
string MyBatchFile = @"C:\Program Files (x86)\MybatchFile.bat";
string _sourcePath = @"C:\FolderToCopy";
string _tempTargetPath = @"C:\TargetFolder\";
var process = new Process {
StartInfo = {
Arguments = string.Format("{0} {1}",
_sourcePath,
_tempTargetPath)
}
};
process.StartInfo.FileName = MyBatchFile;
bool b = process.Start();
I expect this to copy the source files to target location. But nothing happens. My console window also does not stay for enough time so that I can see the error. Can anyone guide to achieve this. I am new in batch files processing.
Edit
By adding a pause
in the end of batch file. Able to reproduce error. Getting error as
Files not found - Program
Running batch file directly does work fine. Just now noticed......when source path has any spaces....I am getting error
bat
files are scripts processed withcmd.exe
. Simply try to runcmd.exe
with/C
key. – Benignpause
command to the end of the batch file and then it will wait for you to press a key before exiting – Greenlawprocess.StartInfo.FileName = @"cmd.exe /c " + MyBatchFile
?? – HellionProcess
uses default programs if the filename is a file and not a program. – Kongstring MyBatchFile = @"\"C:\Program Files (x86)\MybatchFile.bat\"";
(note the \" around the whole expression) – Kongstring MyBatchFile = @"C:\Program~1\MybatchFile.bat"
– Heinrike@
before the string, you should usestring MyBatchFile = @"""C:\Program Files (x86)\MybatchFile.bat""";
. Next time, as a suggestion, a little more details would help. – Kong