Click here to Skip to main content
15,868,087 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to execute powershell to copy some files from one server to another in .Net.
I have executed copy-item command using system.diagnostics.process from the C#. But When the Copying of files completes the Powershell Remains Open. and I have to End the Powershell Manually from Task Manager.
For Each and every files a separate powershell process is generating so i want to end every powershell process after successfully copying. Plz tell me how to do that.

My Method to Copy the files with Powershell

C#
private static void copy(string source,string dest)
        {

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "powershell.exe";
            //dest = @"D$\";
            dest = dest.Remove(1, 1).Insert(1, "$");
            dest = dest + "\\";
            
            string server = "10.0.4.42";
            string xx = "copy-item -path " + source + " \\\\" + server + "\\" + dest + " -Recurse -Force";
            startInfo.Arguments = xx;
            //string ex = "exit";
            //startInfo.Arguments = ex;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            process.StartInfo = startInfo;
            process.Start();
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900