Thursday, February 18, 2010

C# X-Copy Code

So, there is a C# windows "smart client" which I "deploy" from time to time by emailing a zip file to various individuals and instruct them to copy / paste those files to the proper place on their server. Sometimes, somehow, this process gets screwed up. So, I am working on a little utility that will theoretically just let them hit a button and have the files copied correctly to the right server path.

Here are a couple of links that helped me figure this out:
http://www.codeproject.com/KB/cs/wincmdline.aspx
http://commandwindows.com/xcopy.htm

Here is the basic code I came up with. This is not final code. I'm not sure if it works or not, but it is close. I've hard coded the paths, but obviously, you can grab that from an app.config or some other mechanism.

private static void DoCopy()
  {
   //The "/C" Tells Windows to Run The Command then Terminate 
   string strCmdLine;
   string folderSource = @"F:\_AAData\AES_Installations\AES_2010_01_11(3_13_0_1)\Silent Auction Software";
   string folderDest = @"\\devserv2\Temp\tst";
   if (System.IO.Directory.Exists(folderDest))
   {
    strCmdLine = string.Format(@"/C xcopy ""{0}"" ""{1}"" /R /E /Y /H /I", folderSource, folderDest);


    //Declare and instantiate a new process component.
    System.Diagnostics.Process process1;
    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("CMD.exe", strCmdLine);
    process1 = new System.Diagnostics.Process();
    process1.StartInfo = startInfo;
    //Do not receive an event when the process exits.
    process1.EnableRaisingEvents = false;
    process1.Start();
    process1.WaitForExit();
    process1.Close();
   }
   System.Text.StringBuilder sbError = new System.Text.StringBuilder();
   //Need to check that files got properly deployed.
   if (!System.IO.File.Exists(folderDest + @"\publish.htm"))
   {
    sbError.AppendLine("Failed to copy:  " + folderDest + @"\publish.htm");
   }
   //AuctionClient.application
   if (!System.IO.File.Exists(folderDest + @"\AuctionClient.application"))
   {
    sbError.AppendLine("Failed to copy:  " + folderDest + @"\AuctionClient_3_13_0_1.application");
   }
   if (!System.IO.File.Exists(folderDest + @"\AuctionClient_3_13_0_1.application"))
   {
    sbError.AppendLine("Failed to copy:  " + folderDest + @"\AuctionClient_3_13_0_1.application");
   }
   if (!System.IO.File.Exists(folderDest + @"\AuctionClient_3_13_0_1\AuctionClient.exe.config.deploy"))
   {
    sbError.AppendLine("Failed to copy:  " + folderDest + @"\AuctionClient_3_13_0_1\AuctionClient.exe.config.deploy");
   }
   else
   {
    try
    {
     System.Xml.XmlDocument doc = null;
     doc = new System.Xml.XmlDocument();
     doc.Load(folderDest + @"\AuctionClient_3_13_0_1\AuctionClient.exe.config.deploy");
     System.Xml.XmlNode node = doc.SelectSingleNode("//connectionStrings");
     System.Xml.XmlNode connectionStringNode = node.SelectSingleNode("//add[@name='dbConnectionString']");
     string dbConnectionString = connectionStringNode.Attributes["connectionString"].Value;
     sbError.AppendLine(dbConnectionString);
    }
    catch (Exception ex)
    {
     sbError.AppendLine(ex.Message);
    }

   }
   if (!System.IO.File.Exists(folderDest + @"\AuctionClient_3_13_0_1\AuctionClient.exe.deploy"))
   {
    sbError.AppendLine("Failed to copy:  " + folderDest + @"\AuctionClient_3_13_0_1\AuctionClient.exe.deploy");
   }
   string error = sbError.ToString();

  }

No comments: