Click here to Skip to main content
13,086,662 members (56,248 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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 2: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 |
Web02 | 2.8.170813.1 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2017
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