1

I have a bat file, which copies files from one directory to other. If this bat file is clicked manually, files are copied succesfully and no issues with that. But if bat file is executed through C#, "files not found" message is displayed in the cmd window.

Here is my bat file.

echo off 
echo. 
XCOPY "..\SourceFolder\CaSourceFile" "..\DestinationFolder\SubFolder" /r /Y /i /F

if full path is given in bat file, then files are copied successfully. example,

XCOPY "D:\RootFolder\SourceFolder\CaSourceFile" "D:\RootFolder\DestinationFolder\SubFolder" /r /Y /i /F

My C# code:

ProcessStartInfo processInfo = new ProcessStartInfo(batchFile);
                processInfo.UseShellExecute = true;
                Process batchProcess = new Process();
                batchProcess.StartInfo.FileName = "cmd.exe";
                batchProcess.StartInfo = processInfo;
                batchProcess.StartInfo.Arguments = String.Format("\"{0}\" \"{1}\"", @"D:\Europa\Test Release Tool\SingleExeInstaller\EuropaApplication", @"D:\Europa\Test Release Tool\SingleExeInstaller\EuropaInstaller");
                batchProcess.StartInfo.RedirectStandardInput = true;
                batchProcess.StartInfo.RedirectStandardOutput = true;
                batchProcess.StartInfo.RedirectStandardError = true;
                batchProcess.Start();
                batchProcess.WaitForExit();

My application exe is available in C: and bat file is available in D: Is there any idea to over come this issue ? Kindly help me out.

aphoria
  • 19,796
  • 7
  • 64
  • 73
  • From what directory are you executing the C# program? By default this will be the current directory from which it interprets relative paths. – adv12 May 21 '14 at 13:15
  • possible duplicate of [.NET Process.Start default directory?](http://stackoverflow.com/questions/114928/net-process-start-default-directory) – Stefan May 21 '14 at 13:19

4 Answers4

3

You need to set the WorkingDirectory on the StartInfo; your relative paths in the batch file are off because of that. This should be the path to the batch file likely.

batchProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFile);
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
2

The paths you are trying to use are Relative paths, the question is what are they relative to.

When you run the bat file from the command line they will be relative to the current directory which is what you set using the cd command and is likely the directory your .bat file resides in. When you run the bat from your C# exe file the current directory will be the directory your exe file is in.

The current directory is also known as the WorkingDirectory. In order to use a different directory try setting the WorkingDirectory property on the ProcessStartInfo object.

Martin Brown
  • 24,692
  • 14
  • 77
  • 122
1

I expect you need to set the working directory in the StartInfo See .NET Process.Start default directory?

Community
  • 1
  • 1
William Walseth
  • 2,803
  • 1
  • 23
  • 25
0

It's easier to copy the files from one folder to another via C#.

Easy example:

using System.IO;

string sourcePath = "C:\test";
string targetPath = "D:\test_new";
if (!Directory.Exists(targetPath)) {
Directory.CreateDirectory(targetPath);
}
foreach (var srcPath in Directory.GetFiles(sourcePath)) {
//Copy the file from sourcepath and place into mentioned target path, 
//Overwrite the file if same file is exist in target path
File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true);
}

More details about file.copy you can find on MSDN as usual ))) - http://msdn.microsoft.com/en-us/library/system.io.file.copy.aspx

QArea
  • 4,955
  • 1
  • 12
  • 22