Ejemplo n.º 1
0
        public static bool Available(Uri updateUrl)
        {
            // Don't auto-update in the debugger
            if (Debugger.IsAttached)
            {
                // In debug mode we don't want to actually request the file
                Version  = ProcessUtils.ProductVersion;
                Url      = null;
                Comments = "";
                return(false);
            }
            else
            {
                // TODO Display a form that shows we're doing something, otherwise it looks like the application hangs if it takes awhile to do the check
                string AutoUpdateIniFile = StringUtils.PathCombine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProcessUtils.CompanyName, "AutoUpdate.ini");
                using (IniFile AutoUpdateIni = new IniFile(AutoUpdateIniFile))
                {
                    // Check if there's an old installer to delete
                    string OldUrl = AutoUpdateIni.ReadString(ProcessUtils.ProductName, "URL", "");
                    if (OldUrl.Length > 0)
                    {
                        string OldInstaller = StringUtils.PathCombine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProcessUtils.CompanyName, Path.GetFileName(OldUrl));
                        FileUtils.FileDelete(OldInstaller);
                    }

                    // Retrieve the new INI from the updater website
                    string NewIniFile = StringUtils.LFtoCRLF(WebUtils.HttpGet(AutoUpdateIni.ReadString("config", "URL", updateUrl.ToString()) + "?Name=" + WebUtils.UrlEncode(ProcessUtils.ProductName) + "&Version=" + WebUtils.UrlEncode(ProcessUtils.ProductVersion)));
                    if (!string.IsNullOrEmpty(NewIniFile))
                    {
                        // Save and re-open the new INI
                        Directory.CreateDirectory(Path.GetDirectoryName(AutoUpdateIniFile));
                        FileUtils.FileWriteAllText(AutoUpdateIniFile, NewIniFile);
                        using (IniFile NewIni = new IniFile(AutoUpdateIniFile))
                        {
                            // Read the version, url, and comments from the latest ini
                            Version = NewIni.ReadString(ProcessUtils.ProductName, "version", ProcessUtils.ProductVersion);
                            Url     = NewIni.ReadString(ProcessUtils.ProductName, "URL", "");
                            int CommentCount = NewIni.ReadInt32(ProcessUtils.ProductName, "comments", 0);
                            if (CommentCount > 0)
                            {
                                Comments = NewIni.ReadString(ProcessUtils.ProductName, "comment1", "");
                                for (int i = 2; i <= CommentCount; i++)
                                {
                                    Comments += Environment.NewLine + NewIni.ReadString(ProcessUtils.ProductName, "comment" + i.ToString(), "");
                                }
                            }
                            else
                            {
                                Comments = "";
                            }
                        }
                    }
                    else
                    {
                        // The new INI was not found
                        Version  = ProcessUtils.ProductVersion;
                        Url      = null;
                        Comments = "";
                    }
                }

                return(ProcessUtils.ProductVersion != Version);
            }
        }