I need to copy a file from one directory to another and do something with that file. I need to copy it with cmd, rather than File.Copy(), because I need the copy to be done as a part of ProcessStartInfo.
            Asked
            
        
        
            Active
            
        
            Viewed 3,408 times
        
    5
            
            
         
    
    
        Joel Coehoorn
        
- 399,467
- 113
- 570
- 794
 
    
    
        Petar
        
- 273
- 1
- 4
- 16
3 Answers
5
            You can use this code and change startInfo.Arguments, but /C should be!
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new 
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy example.txt backup.txt";
process.StartInfo = startInfo;
process.Start();
 
    
    
        PWND
        
- 409
- 3
- 11
- 
                    1Thanks, `/C` definitely helped! – Petar Nov 14 '18 at 15:49
2
            
            
        You can create a bat-file to copy one or multiple files (using *). Then execute the batch file.
        string batFileName = @"C:\{filePath}\copy.bat";
        System.IO.File.WriteAllText(batFileName, @"copy {fileName}.{extension} {destination-filePath}");
        System.Diagnostics.Process.Start(batFileName);
 
    
    
        AsusT9
        
- 154
- 1
- 11
1
            
            
        I was able to formulate this answer using the DOS Copy syntax along with this Stack Overflow QA Start cmd window and run commands inside
var startInfo = new ProcessStartInfo {
    FileName = "cmd.exe",
    RedirectStandardInput = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true
};
var process = new Process {StartInfo = startInfo};
process.Start();
process.StandardInput.WriteLine(@"copy c:\Source\Original.ext D:\Dest\Copy.ext");
process.StandardInput.WriteLine("exit");
process.WaitForExit();
 
    
    
        Mad Myche
        
- 1,075
- 1
- 7
- 15