5

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.

0

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, /C definitely helped!
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);

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.