Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 95,475 C# Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 954 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C# Expert

Register to Make This Box Go Away!



Perform an XCOPY in C#

This is a short snippet that performs an XCOPY in C# (Similar to the System() Function in C++)

Submitted By: PsychoCoder
Actions:
Rating:
Views: 3,652

Language: C#

Last Modified: August 1, 2007
Instructions: the line string XCopyArguments = "yourargumentshere" - replace "yourargumentshere" with the desired XCOPY arguments

Snippet


  1. public bool ProcessXCopy()
  2. {
  3.     string XCopyArguments = "yourargumentshere";
  4.     Process XCopyProcess = new Process();
  5.     ProcessStartInfo XCopyStartInfo = new ProcessStartInfo();
  6.  
  7.     XCopyStartInfo.FileName = "CMD.exe ";
  8.  
  9.     //do not write error output to standard stream
  10.     XCopyStartInfo.RedirectStandardError = false;
  11.     //do not write output to Process.StandardOutput Stream
  12.     XCopyStartInfo.RedirectStandardOutput = false;
  13.     //do not read input from Process.StandardInput (i/e; the keyboard)
  14.     XCopyStartInfo.RedirectStandardInput = false;
  15.  
  16.     XCopyStartInfo.UseShellExecute = false;
  17.     //Dont show a command window
  18.     XCopyStartInfo.CreateNoWindow = true;
  19.  
  20.     XCopyStartInfo.Arguments = "/D /c XCOPY " + XCopyArguments;
  21.  
  22.     XCopyProcess.EnableRaisingEvents = true;
  23.     XCopyProcess.StartInfo = XCopyStartInfo;
  24.  
  25.     //start cmd.exe & the XCOPY process
  26.     XCopyProcess.Start();
  27.  
  28.     //set the wait period for exiting the process
  29.     XCopyProcess.WaitForExit(15000); //or the wait time you want
  30.  
  31.     int ExitCode = XCopyProcess.ExitCode;
  32.     bool XCopySuccessful = true;
  33.  
  34.     //Now we need to see if the process was successful
  35.     if (ExitCode > 0 & !XCopyProcess.HasExited)
  36.     {
  37.         XCopyProcess.Kill();
  38.         XCopySuccessful = false;
  39.     }
  40.  
  41.     //now clean up after ourselves
  42.     XCopyProcess.Dispose();
  43.     StartInfo = null;
  44.     return XCopySuccessful;
  45. }

Copy & Paste


Comments


csharp 2007-11-30 04:49:23

I am using the above code to xcopy files from one machine to other.It does not copy files but also does not give any error.Process completes without doing any copy.It works to copy files on same machine protected void copyfunction() { }

csharp 2007-11-30 04:50:36

I am using the above code to xcopy files from one machine to other.It does not copy files but also does not give any error.Process completes without doing any copy.It works to copy files on same machine protected void copyfunction() { string source, destination, param; source = @"C:\ot\XcopyTrial"; destination = @"\\\Dircopy"; param = @"/E /H /R /Y"; string xCopyArguments = source + " " + destination + " " + param; // if (ProcessXCopy(xCopyArguments) == true) // { }

csharp 2007-11-30 04:51:09

I am using the above code to xcopy files from one machine to other.It does not copy files but also does not give any error.Process completes without doing any copy.It works to copy files on same machine protected void copyfunction() { string source, destination, param; source = @"C:\ot\XcopyTrial"; destination = @"\\\Dircopy"; param = @"/E /H /R /Y"; string xCopyArguments = source + " " + destination + " " + param; if (ProcessXCopy(xCopyArguments) == true) { } }

csharp 2007-11-30 04:56:06

I am using the code in windows service.Code works for copying files on same machine.But when destination is other machine it does not work in windows service.It works fine in windows application


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->