C#自动更新程序

C#自动更新程序,c#,xml,C#,Xml,好的,我正在开发一个程序,如果版本不匹配,它会自动下载更新。 现在的问题是,它从XML文件加载信息,但不下载指定的文件 currentFile = string.Empty; label1.Text = "Preparing to download file(s)..."; Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread be

好的,我正在开发一个程序,如果版本不匹配,它会自动下载更新。 现在的问题是,它从XML文件加载信息,但不下载指定的文件

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();
欢迎对代码提出任何改进建议

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();
以下是完整的源代码:

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();
提前感谢您的帮助

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();
编辑 Program.cs中指定的要下载的文件无法下载,程序会在

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();
然后开始“线程t”

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();
似乎正在产生问题的代码:

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();
UpdateForm.cs

currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.InteropServices;

namespace LauncherBeta1
{
    public partial class UpdateForm : Form
    {
        const int MF_BYPOSITION = 0x400;
        [DllImport("User32")]
        private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
        [DllImport("User32")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        [DllImport("User32")]
        private static extern int GetMenuItemCount(IntPtr hWnd);



        private static WebClient webClient = new WebClient();
        internal static List<Uri> uriFiles = new List<Uri>();
        internal static Uri uriChangelog = null;
        private static long fileSize = 0, fileBytesDownloaded = 0;
        private static Stopwatch fileDownloadElapsed = new Stopwatch();
        bool updateComplete = false, fileComplete = false;
        string currentFile = string.Empty, labelText = string.Empty;
        int progbarValue = 0, KBps = 0;

        public UpdateForm()
        {
            IntPtr hMenu = GetSystemMenu(this.Handle, false);
            int menuItemCount = GetMenuItemCount(hMenu);
            RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);

            InitializeComponent();

            XmlDocument xdoc = new XmlDocument();
            xdoc.Load("http://raiderz.daregamer.com/updates/app_version.xml");
            XmlNode xNodeVer = xdoc.DocumentElement.SelectSingleNode("Version");
            FileVersionInfo fileVer = FileVersionInfo.GetVersionInfo(AppDomain.CurrentDomain.BaseDirectory + "lua5.1.dll");
            int ver_app = Convert.ToInt32(fileVer.FileVersion.ToString());
            int ver_xml = Convert.ToInt32(xNodeVer);

            if (ver_xml == ver_app)
            {
                Application.Run(new Form1());
                Environment.Exit(0);
            }
            else
            {
                if (ver_xml < ver_app || ver_xml > ver_app)
                {
                    if (uriChangelog != null)
                    {
                        label1.Text = "Status: Downloading changelog...";
                        try
                        {
                            string log = webClient.DownloadString(uriChangelog);
                            log = log.Replace("\n", Environment.NewLine);
                            txtboxChangelog.Text = log;
                        }
                        catch (WebException ex) { }
                    }
                    foreach (Uri uri in uriFiles)
                    {
                        string uriPath = uri.OriginalString;
                        currentFile = uriPath.Substring(uriPath.LastIndexOf('/') + 1);
                        if (File.Exists(currentFile))
                        {
                            label1.Text = "Status: Deleting " + currentFile;
                            File.Delete(currentFile);
                        }
                    }
                    currentFile = string.Empty;
                    label1.Text = "Preparing to download file(s)...";
                    Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                    t.Start();
                }
                else
                {
                    //MessageBox.Show("Client is up to date!");
                    Application.Run(new Form1());
                    Environment.Exit(0);
                }
            }
        }

        private void DownloadFiles()
        {

            foreach (Uri uri in uriFiles)
            {
                try
                {
                    fileComplete = false;
                    fileDownloadElapsed.Reset();
                    fileDownloadElapsed.Start();
                    string uriPath = uri.OriginalString;
                    currentFile = uriPath.Substring(uriPath.LastIndexOf('/') + 1);
                    webClient.DownloadFileAsync(uri, currentFile);
                    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadFileCompleted);
                    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
                    while (!fileComplete) { Thread.Sleep(1000); }
                }
                catch { continue; }
            }
            currentFile = string.Empty;
            updateComplete = true;
        }

        void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progbarValue = e.ProgressPercentage;
            fileSize = e.TotalBytesToReceive / 1024;
            fileBytesDownloaded = e.BytesReceived / 1024;
            if (fileBytesDownloaded > 0 && fileDownloadElapsed.ElapsedMilliseconds > 1000)
            {
                KBps = (int)(fileBytesDownloaded / (fileDownloadElapsed.ElapsedMilliseconds / 1000));
            }
            labelText = "Status: Downloading " + currentFile +
                        "\n" + fileBytesDownloaded + " KB / " + fileSize + " KB - " + KBps + " KB/s";
        }

        void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            progbarValue = 0;
            fileComplete = true;
        }

        /// <summary>
        /// Returns file size (Kb) of a Uri
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        private long GetFileSize(Uri uri)
        {
            try
            {
                WebRequest webRequest = HttpWebRequest.Create(uri);
                using (WebResponse response = webRequest.GetResponse())
                {
                    long size = response.ContentLength;
                    return size / 1024;
                }
            }
            catch { return 1; }
        }

        private void timerMultiPurpose_Tick(object sender, EventArgs e)
        {
            if (updateComplete == true)
            {
                updateComplete = false;
                label1.Text = "Status: Complete";
                progressBar1.Value = 0;
                MessageBox.Show("Update complete!!");
                Application.Run(new Form1());
                Environment.Exit(0);
            }
            else
            {
                progressBar1.Value = progbarValue;
                label1.Text = labelText;
            }
        }

        private void UI_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }
    }
}

我没有下载或查看您的代码,但如果这些代码都是基于C#的,并且基于v2.0或更高版本的框架,您可能希望使用ClickOnce进行登录。它将为您处理大部分自动更新,甚至允许用户在脱机且无法连接到您的更新服务器时继续使用该程序。

病毒扫描仍在队列中…>。请在此处发布相关代码片段,并准确指出哪些部分不起作用,您希望它做什么,以及您遇到的任何异常/编译器错误。我们无法检查您的整个代码库是否存在潜在问题。在这里发布的代码相当大。。。另外,如果您在VS中打开项目,您可以更好地了解我的代码中发生了什么。您需要找出代码中与问题相关的部分,并将其发布在此处。要求我们审查你的整个应用程序真是太冒昧了。如果你不知道问题出在哪里,那么我们为什么要比你花更多的精力调试它呢?在这里发布相关摘录的另一个原因(除了出于对那些你想帮助你的人的礼貌):这些链接会永远可用吗?甚至6个月后?StackOverflow的部分价值在于,问题会持续很多年,帮助了很多有类似问题的人。为了让这个问题在5年或10年后变得有意义,这个问题的所有部分都必须在一个位置。这个程序是一个具有集成自动下载功能的启动器。我认为ClickOnce不适合我想要实现的目标。
currentFile = string.Empty;
                label1.Text = "Preparing to download file(s)...";
                Thread t = new Thread(new ThreadStart(DownloadFiles)); // Making a new thread because I prefer downloading 1 file at a time
                t.Start();