C# • Implement a Web Service Updater.

Listing 1. One of the key components in this solution that enables you to signal whether you need to update a given app created with the Compact Framework. The specific tasks accomplished by the Web service are straightforward. Essentially, the Web service determines whether a given application needs to be updated, and if so, determines which files need to be updated. The Web service then provides the necessary files to complete the required update.

[WebMethod]
public string GetBinaries(string FileListXml)
{
   XmlDocument xDoc = new XmlDocument();
   xDoc.LoadXml(FileListXml);
   System.Xml.XmlAttribute xa; 
   
   foreach(XmlNode n in xDoc.DocumentElement.ChildNodes)
   {
      xa = xDoc.CreateAttribute("value");
      xa.Value = GetBase64StringFromFile
         (n.Attributes.GetNamedItem("name").Value);
      //add the base64string to the node
      n.Attributes.Append(xa);
   }

   //send back the document as we received it, with 
   //the addition of the base64 binary strings
   return xDoc.InnerXml;
}   

private string GetBase64StringFromFile(string file)
{
   //Open the file, and load it into the FileStream
   FileStream inFile = 
      new FileStream(file,FileMode.Open,
      FileAccess.Read,FileShare.Read);
   byte[] binaryData = new Byte[inFile.Length];
   long bytesRead = inFile.Read(binaryData, 0,(int)inFile.Length);
   inFile.Close();

   //Convert the byte array to a base64 string
   return System.Convert.ToBase64String(
      binaryData,0,binaryData.Length);
}