Beispiel #1
0
        //**************************************************************
        // Load()
        //**************************************************************
        public void Load(string url)
        {
            _url = url;
            String LocalManifestPath = AppDomain.CurrentDomain.BaseDirectory +
                                       Path.GetFileName((new Uri(_url)).LocalPath);

            WebFileLoader.UpdateFile(_url, LocalManifestPath);

            _manifest = new XmlDocument();
            _manifest.Load(LocalManifestPath);

            ApplicationUrl   = _manifest.GetElementsByTagName("ApplicationUrl")[0].InnerText;
            AvailableVersion = _manifest.GetElementsByTagName("AvailableVersion")[0].InnerText;

            try
            {
                XmlNode     filelist = _manifest.GetElementsByTagName("FilesToDownload")[0];
                XmlNodeList files    = null;
                if (filelist != null && filelist.HasChildNodes)
                {
                    files = filelist.ChildNodes;
                }

                if (files != null)
                {
                    _filesToDownload = new string[files.Count];
                    for (int i = 0; i <= files.Count; i++)
                    {
                        _filesToDownload[i] = files[i].InnerText;
                    }
                }
            }catch {}
        }
Beispiel #2
0
        //**************************************************************
        // UpdateFileList()
        // - A multi-file UpdateFile
        //**************************************************************
        public static void UpdateFileList(SortedList fileList, string sourceUrl, string destPath)
        {
            Resource currentResource;

            //If the directory doesn't exist, create it first
            if (!Directory.Exists(destPath))
            {
                Directory.CreateDirectory(destPath);
            }

            foreach (Object o in fileList)
            {
                currentResource = (Resource)(((DictionaryEntry)o).Value);

                string url      = sourceUrl + currentResource.Name;
                string FilePath = destPath + currentResource.Name;
                WebFileLoader.UpdateFile(url, FilePath);
            }
        }
Beispiel #3
0
        //**************************************************************
        // Download()
        //**************************************************************
        private void Download()
        {
            bool DownloadInProgress   = true;
            int  DownloadAttemptCount = 0;
            int  SecondsToSleep       = 0;

            while (DownloadInProgress)
            {
                Thread.Sleep(TimeSpan.FromSeconds(SecondsToSleep));
                SecondsToSleep = SecondsBetweenDownloadRetry;

                DownloadAttemptCount++;

                Trace.WriteLine("APPMANAGER:  Attempting to download update from:  " + AppMan.Manifest.State.DownloadSource);

                try
                {
                    int UpdateCount = 0;

                    //if we have a known set of files-to-download, get it from that. otherwise, deep copy the directory - krishna - jun05
                    if (AppMan.Manifest.State.FilesToDownload != null && AppMan.Manifest.State.FilesToDownload.Length != 0)                     //
                    {
                        //raise the progress event before starting first download.
                        if (OnDownloadProgress != null)
                        {
                            //currentState represents the percent done.
                            DownloadEventArgs.CurrentState = 0;
                            DownloadEventArgs.CurrentFile  = 1;
                            DownloadEventArgs.TotalFiles   = AppMan.Manifest.State.FilesToDownload.Length;
                            OnDownloadProgress(this, DownloadEventArgs);
                        }

                        for (int i = 0; i < AppMan.Manifest.State.FilesToDownload.Length; i++)
                        {
                            string file = AppMan.Manifest.State.FilesToDownload[i];
                            string url  = AppMan.Manifest.State.DownloadSource;
                            string destinationtempDir = AppMan.Manifest.State.DownloadDestination;
                            Debug.WriteLine("URL = " + url);
                            if (!url.EndsWith("/"))
                            {
                                url += "/";
                            }
                            url += file;
                            Debug.WriteLine("URL after = " + url);

                            //make sure the path has a \ at the end
                            MakeValidPath(destinationtempDir);

                            //If the directory doesn't exist, create it first
                            if (!Directory.Exists(destinationtempDir))
                            {
                                Directory.CreateDirectory(destinationtempDir);
                            }

                            //copy file to temp location, not to the main place.
                            file = destinationtempDir + file;
                            Debug.WriteLine("File temp path= " + file);

                            WebFileLoader.UpdateFile(url, file);
                            //raise the event
                            UpdateCount++;
                            if (OnDownloadProgress != null)
                            {
                                //currentState represents the percent done.
                                if (i < AppMan.Manifest.State.FilesToDownload.Length)
                                {
                                    DownloadEventArgs.CurrentFile = i + 1;
                                }
                                else
                                {
                                    DownloadEventArgs.CurrentFile = i;
                                }
                                //total files is already set in the code above.
                                //set the percentage done,below.
                                DownloadEventArgs.CurrentState = (100 * UpdateCount / AppMan.Manifest.State.FilesToDownload.Length);
                                OnDownloadProgress(this, DownloadEventArgs);
                            }
                        }
                    }
                    else
                    {
                        UpdateCount = WebFileLoader.CopyDirectory(AppMan.Manifest.State.DownloadSource, AppMan.Manifest.State.DownloadDestination);
                    }

                    Debug.WriteLine("APPMANAGER:  Number of files updated from the server:  " + UpdateCount);

                    Debug.WriteLine("APPMANAGER:  App update downloaded successfully");

                    DownloadInProgress = false;
                }
                //Things that could go wrong while downloading are pretty much any kind of
                //network problem, like the client just going offline.  However, this can cause
                //itself to manifest in any number of ways... like exceptions on the stream
                //objects used to copy the file to disk.
                catch (WebException e)
                {
                    Debug.WriteLine("APPMANAGER:  Update download failed due to network exception:");
                    Debug.WriteLine("APPMANAGER:  " + e.ToString());

                    if (DownloadAttemptCount >= DownloadRetryAttempts)
                    {
                        Debug.WriteLine("APPMANAGER:  Download attempt has failed 3 times.  Aborting Update");
                        UpdateEventArgs.ErrorMessage = "Download of a new update from '" + AppMan.Manifest.State.DownloadSource
                                                       + "' failed with the network error:  " + e.Message;
                        throw e;
                    }
                }
                catch (IOException e)
                {
                    Debug.WriteLine("APPMANAGER:  Update download failed due to file I/O exception:");
                    Debug.WriteLine("APPMANAGER:  " + e.ToString());

                    if (DownloadAttemptCount >= DownloadRetryAttempts)
                    {
                        Debug.WriteLine("APPMANAGER:  Download attempt has failed 3 times.  Aborting Update");
                        UpdateEventArgs.ErrorMessage = "Saving the new update to disk at '" + AppMan.Manifest.State.DownloadDestination
                                                       + "' failed with the following error:  " + e.Message;
                        throw e;
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine("APPMANAGER:  Update download failed due to the following error:  " + e.Message);
                    Debug.WriteLine("APPMANAGER:  " + e.ToString());

                    if (DownloadAttemptCount >= DownloadRetryAttempts)
                    {
                        Debug.WriteLine("APPMANAGER:  Download attempt has failed 3 times.  Aborting Update");
                        UpdateEventArgs.ErrorMessage = "Update failed with the following error:  '" + e.Message + "'";
                        throw e;
                    }
                }
            }
        }
Beispiel #4
0
        //**************************************************************
        // OnAssemblyResolve()
        // - This code is what does the auto-download of missing files
        //**************************************************************
        private Assembly OnAssemblyResolve(Object sender, ResolveEventArgs args)
        {
            //Check to see if the AssemblyLoad in this event is what caused the
            //event to fire again.  If so, the load failed.
            if (LoadingAssembly == true)
            {
                return(null);
            }

            LoadingAssembly = true;

            string[] AssemblyNameParts = args.Name.Split(new Char[] { ',' }, 2);
            string   AssemblyName      = AssemblyNameParts[0] + ".dll";
            string   FilePath          = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AssemblyName);

            string url;

            if (ChangeDetectionMode == ChangeDetectionModes.DirectFileCheck)
            {
                url = UpdateUrl + AssemblyName;
            }
            else
            {
                ServerManifest SM = new ServerManifest();
                SM.Load(UpdateUrl);
                url = Path.Combine(SM.ApplicationUrl, AssemblyName);
            }

            Debug.WriteLine("APPMANAGER:  Auto-downloading assembly:  " + AssemblyName + ".  From:  " + url);

            try
            {
                WebFileLoader.UpdateFile(url, FilePath);
            }
            catch (Exception e)
            {
                Debug.WriteLine("APPMANAGER:  Failed to download the missing assembly from the web server.");
                Debug.WriteLine("APPMANAGER:  " + e.ToString());
                if (ShowDefaultUI)
                {
                    MessageBox.Show("Unable to auto-download the missing parts of the application from:\r\n"
                                    + url + "\r\n\r\n"
                                    + "Make sure your connected to the network.  If the problem persists re-install the application.");
                }
                return(null);
            }

            Assembly assembly;

            try
            {
                assembly = Assembly.Load(args.Name);
            }
            catch (Exception e)
            {
                Debug.WriteLine("APPMANAGER:  Failed to load the auto-downloaded assembly.");
                Debug.WriteLine("APPMANAGER:  " + e.ToString());
                return(null);
            }
            finally
            {
                LoadingAssembly = false;
            }

            return(assembly);
        }