c#.NET executing shell commands

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    c#.NET executing shell commands

    Hi all ...

    I am trying to execute xcopy command from visual c# express 2005 . My code is as follows :

    Code:
    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"cmd.exe /c xcopy C:\Source C:\Destination  /s");
                psi.UseShellExecute = false;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardInput = true;
                psi.RedirectStandardError = true;
                System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
    This keeps giving me error "Win32 exception was unhandled" File not found . Both source and destination directories exist. the same command gets executed if i try to execute it from command prompt .
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Why do you run with cmd.exe ?
    You should be doing like this:
    [code=c#]
    System.Diagnost ics.ProcessStar tInfo psi = new System.Diagnost ics.ProcessStar tInfo(@"xcopy C:\Source C:\Destination /s");
    psi.UseShellExe cute = true;
    psi.RedirectSta ndardOutput = true;
    psi.RedirectSta ndardInput = true;
    psi.RedirectSta ndardError = true;
    System.Diagnost ics.Process proc = System.Diagnost ics.Process.Sta rt(psi);
    [/code]

    I also think you might need to end your source and dest directories with a \ as well

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Why do you want to do this with the shell anyway? There are built in FileInfo and DirectoryInfo objects that will let you do this naively in managed code.

      Comment

      • rhitam30111985
        New Member
        • Aug 2007
        • 112

        #4
        Originally posted by insertAlias
        Why do you want to do this with the shell anyway? There are built in FileInfo and DirectoryInfo objects that will let you do this naively in managed code.

        well directory class is there for the purpose but the same cant be used for copying files for overwriting when the files are read only ... in that case an xcopy command has to be excecuted from the shell with correct switches .

        Comment

        Working...