Beispiel #1
0
        /// <summary>
        /// copy file or directory </summary>
        public static bool Copy(string SourcePath, string DestinationPath, CopyProgressDelegate callback = null)
        {
            try
            {
                if (Directory.Exists(SourcePath))
                {
                    foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
                    {
                        Os.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
                    }

                    foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
                    {
                        CopyByBlock(newPath, newPath.Replace(SourcePath, DestinationPath), callback);
                    }
                }
                else if (File.Exists(SourcePath))
                {
                    CopyByBlock(SourcePath, Os.Combine(DestinationPath, Os.GetFileName(SourcePath)), callback);
                }
                return(true);
            }
            catch (Exception ex)
            {
                Program.log.Write(ex.Message);
                return(false);
            }
        }
Beispiel #2
0
        /*************************************************************************************************************************/

        /// <summary>
        /// load global config file from portable file configuration or global file configuration
        /// </summary>
        /// <param name="parameters">reference to parameter object</param>
        public ProgramOptionsFile(ProgramOptions programOptions)
        {
            this.programOptions = programOptions;

            // use local config file
            this.optionsFilePath = Os.Combine(Os.GetCurrentApplicationDirectory(), this.configFileName);

            // use global config file if local version not exist
            if (!Os.FileExists(this.optionsFilePath))
            {
                this.optionsFilePath = Os.Combine(
                    this.GetGlobalConfigDirectory(),
                    this.configFileName
                    );
            }

            // open config file if exist
            if (Os.FileExists(this.optionsFilePath))
            {
                this.LoadConfigFile();
            }
            else
            {
                string globalConfigDirectory = Os.Combine(
                    Os.GetApplicationsDirectory(),
                    this.configFileDirectory
                    );

                // create global config directory if not exist
                if (!Os.DirectoryExists(globalConfigDirectory))
                {
                    Os.CreateDirectory(globalConfigDirectory);
                }

                // if config file dosn't exist create one with default values
                this.SaveConfigFile();
            }
        }
Beispiel #3
0
        public void CheckUpdates(bool showCurrentVersionStatus = false)
        {
            Job.DoJob(
                new DoWorkEventHandler(
                    delegate(object o, DoWorkEventArgs args)
            {
                try
                {
                    string currentVersion = Os.GetThisAssemblyVersion();
                    Program.log.Write("CheckUpdates current version: " + currentVersion);

                    string lastVersion = Network.GetWebPage(this.homepage + this.lastversionFile);

                    Program.log.Write("CheckUpdates last version: " + lastVersion);

                    if (lastVersion == null)
                    {
                        return;
                    }

                    lastVersion = lastVersion.TrimEnd('\r', '\n').Trim();


                    Version localVersion  = new Version(currentVersion);
                    Version serverVersion = new Version(lastVersion);

                    if (serverVersion.CompareTo(localVersion) == 1)
                    {
                        string signature = Network.GetWebPage(this.homepage + this.signatureFile);
                        signature        = signature.TrimEnd('\r', '\n').Trim();

                        if (signature == null || signature.Length < 64)
                        {
                            return;
                        }

                        UpdateForm updateForm = new UpdateForm();
                        updateForm.ShowDialog();

                        if (updateForm.CanUpdate())
                        {
                            string tempPath       = Os.Combine(Os.GetTempPath(), this.updateFolderName);
                            string executablePath = tempPath + Os.GetSeparator() + this.updateExecutableName;
                            Os.RemoveDirectory(tempPath);
                            Os.CreateDirectory(tempPath);

                            string downloadFromUrl = installationUrl.Replace("{VERSION}", lastVersion);

                            Program.log.Write("CheckUpdates downloading: " + downloadFromUrl);

                            Network.DownloadFile(downloadFromUrl, executablePath);

                            string downloadedFileChecksum = Hash.GetFileHash(executablePath);

                            if (downloadedFileChecksum == signature)
                            {
                                Os.RunProcess(executablePath);
                            }
                            else
                            {
                                Program.log.Write("CheckUpdates error: invalid signature");
                            }
                        }
                        else if (updateForm.SkipVersion())
                        {
                        }
                    }
                    else
                    {
                        if (showCurrentVersionStatus)
                        {
                            MessageBox.Show("You have last version.");
                        }
                    }
                } catch (Exception ex) {
                    Program.log.Write("CheckUpdates error: " + ex.Message);
                }
            }
                    ),
                new RunWorkerCompletedEventHandler(
                    delegate(object o, RunWorkerCompletedEventArgs args)
            {
            }
                    )
                );
        }
        /// <summary>
        /// decompress string with directory structure to path</summary>
        public static void DecompressPath(string compressedData, string destinationPath)
        {
            if (!Os.DirectoryExists(destinationPath))
            {
                return;
            }

            destinationPath = Os.NormalizedFullPath(destinationPath);

            string xml = Unzip(compressedData);

            XmlReaderSettings xws = new XmlReaderSettings
            {
                CheckCharacters = false
            };

            string            version     = "";
            List <EDirectory> directories = new List <EDirectory>();
            List <EFile>      files       = new List <EFile>();

            try
            {
                using (XmlReader xr = XmlReader.Create(new StringReader(xml), xws))
                {
                    XElement xRoot = XElement.Load(xr);
                    if (xRoot.Name.ToString() == "archive")
                    {
                        foreach (XElement xEl in xRoot.Elements())
                        {
                            if (xEl.Name.ToString() == "version")
                            {
                                version = xEl.Value;
                            }

                            if (xEl.Name.ToString() == "directories")
                            {
                                foreach (XElement xDirectory in xEl.Descendants())
                                {
                                    if (xDirectory.Name.ToString() == "directory")
                                    {
                                        string name = "";

                                        foreach (XElement xData in xDirectory.Descendants())
                                        {
                                            if (xData.Name.ToString() == "name")
                                            {
                                                name = xData.Value;
                                            }
                                        }

                                        if (name.Trim() != "")
                                        {
                                            EDirectory eDirectory = new EDirectory
                                            {
                                                name = name
                                            };
                                            directories.Add(eDirectory);
                                        }
                                    }
                                }
                            }

                            if (xEl.Name.ToString() == "files")
                            {
                                foreach (XElement xFile in xEl.Descendants())
                                {
                                    if (xFile.Name.ToString() == "file")
                                    {
                                        string name = "";
                                        string data = "";

                                        foreach (XElement xData in xFile.Descendants())
                                        {
                                            if (xData.Name.ToString() == "name")
                                            {
                                                name = xData.Value;
                                            }

                                            if (xData.Name.ToString() == "data")
                                            {
                                                data = xData.Value;
                                            }
                                        }

                                        if (name.Trim() != "" && data.Trim() != "")
                                        {
                                            EFile eFile = new EFile
                                            {
                                                name = name,
                                                data = data
                                            };
                                            files.Add(eFile);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Program.log.Write("decompress file xml error: " + ex.Message);
            }

            foreach (EDirectory directory in directories)
            {
                string newDirPath = Os.Combine(destinationPath, directory.name);
                if (!Os.Exists(newDirPath))
                {
                    Os.CreateDirectory(newDirPath);
                }
            }

            foreach (EFile file in files)
            {
                string newFilePath = Os.Combine(destinationPath, file.name);
                if (!Os.Exists(newFilePath))
                {
                    Os.WriteAllBytes(
                        newFilePath,
                        Convert.FromBase64String(
                            file.data
                            )
                        );
                }
            }

            // process dirrectories create to path

            // process files create to path
        }