コード例 #1
0
        private List <DownloadCenterForm.DownloadableFileInfo> removeAlreadyInstalledPackagesFromList(List <DownloadCenterForm.DownloadableFileInfo> packageListToFilter)
        {
            // get all the folders in the parts folder to know what is already installed
            DirectoryInfo partsFolder = new DirectoryInfo(PartLibraryPanel.sFullPathForLibrary);

            DirectoryInfo[] directoriesInPartsFolder = new DirectoryInfo[] { };
            if (partsFolder.Exists)
            {
                directoriesInPartsFolder = partsFolder.GetDirectories();
            }
            else
            {
                // if the part folder doesn't exist, try to create it
                partsFolder.Create();
                // if the part folder doesn't exist we need to get all the package found
                return(packageListToFilter);
            }


            // create a list with only the name of the directory
            List <DownloadCenterForm.DownloadableFileInfo> installedPackages = new List <DownloadCenterForm.DownloadableFileInfo>();

            foreach (DirectoryInfo directory in directoriesInPartsFolder)
            {
                // save the package name in the package info
                DownloadCenterForm.DownloadableFileInfo package = new DownloadCenterForm.DownloadableFileInfo();
                package.FileName = directory.Name + ".zip";                 // add the zip extension to facilitate the string comparison

                // check the about file on the local drive to get the version number
                string aboutFileName = Application.StartupPath + @"/parts/" + directory.Name + @"/config/About.txt";
                if (File.Exists(aboutFileName))
                {
                    package.Version = getPackageVersionInAboutFile(aboutFileName);
                }

                // add the package to the list
                installedPackages.Add(package);
            }

            // then iterate on the list to filter and remove the items already installed that has the good version
            for (int i = 0; i < packageListToFilter.Count; ++i)
            {
                foreach (DownloadCenterForm.DownloadableFileInfo package in installedPackages)
                {
                    if (package.FileName.Equals(packageListToFilter[i].FileName, StringComparison.OrdinalIgnoreCase) && string.Compare(package.Version, packageListToFilter[i].Version) >= 0)
                    {
                        packageListToFilter.RemoveAt(i);
                        i--;
                        break;
                    }
                }
            }

            return(packageListToFilter);
        }
コード例 #2
0
        private void backgroundWorkerSearchOnline_DoWork(object sender, DoWorkEventArgs e)
        {
            const string partsFolder = @"/parts/";

            // Get the BackgroundWorker that raised this event.
            BackgroundWorker worker = sender as BackgroundWorker;
            // and get the parameters
            SearchParameter parameters = e.Argument as SearchParameter;
            ResultParameter result     = new ResultParameter();

            // declare a variable to report the progress on the search
            int currentUrlIndex = 0;

            // iterate on all the url in the search parameters
            foreach (string url in parameters.searchURLs)
            {
                // increase the current url and init the serach step
                currentUrlIndex++;
                int searchStep = 0;

                try
                {
                    // report the begining of the search
                    worker.ReportProgress(searchStep, new ProgressParameter(currentUrlIndex, parameters.searchURLs.Count));

                    // create a web request to browse the url
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        // report that we got an answer
                        worker.ReportProgress(++searchStep, new ProgressParameter(currentUrlIndex, parameters.searchURLs.Count));

                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            string html = reader.ReadToEnd();
                            // use a regexp to parse all the listed files from the html page
                            Regex           regex   = new Regex("<a href=\".+\\.zip\">(?<name>.+\\.zip)</a>");
                            MatchCollection matches = regex.Matches(html);
                            foreach (Match match in matches)
                            {
                                if (match.Success)
                                {
                                    // create an array to store the destination file name, and url source file name
                                    DownloadCenterForm.DownloadableFileInfo downloadInfo = new DownloadCenterForm.DownloadableFileInfo();
                                    // get the file name found
                                    string fileName = match.Groups["name"].Value;
                                    string fileNameWithoutExtension = fileName.Remove(fileName.Length - 4);
                                    // check if the file name contains a version number
                                    int dotIndex = fileNameWithoutExtension.IndexOf('.');
                                    if (dotIndex > 0)
                                    {
                                        downloadInfo.FileName = fileNameWithoutExtension.Remove(dotIndex) + ".zip";
                                        downloadInfo.Version  = fileNameWithoutExtension.Substring(dotIndex + 1);
                                    }
                                    else
                                    {
                                        downloadInfo.FileName = fileName;
                                        downloadInfo.Version  = string.Empty;
                                    }
                                    downloadInfo.SourceURL         = url + fileName;
                                    downloadInfo.DestinationFolder = partsFolder + fileName;
                                    // add the array in the result list
                                    result.allPackageListFound.Add(downloadInfo);
                                }
                            }

                            // report that we have finished to parse the html
                            worker.ReportProgress(++searchStep, new ProgressParameter(currentUrlIndex, parameters.searchURLs.Count));
                        }
                    }
                }
                catch
                {
                    // report that the error
                    worker.ReportProgress(3, new ProgressParameter(currentUrlIndex, parameters.searchURLs.Count));
                }
            }

            // set the result in the event of the background worker
            e.Result = result;
        }