Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PowerShell
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
 
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 29-Mar-13 3:54am

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



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 29 Mar 2013
Copyright © CodeProject, 1999-2014
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