Click here to Skip to main content
11,925,006 members (47,143 online)
Rate this:
Please Sign up or sign in to vote.
See more: PowerShell

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

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 = "";
            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;
Posted 29-Mar-13 3:54am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151125.3 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100