0

我试图做一个可以自动更新的应用程序。但它不起作用。

主要“已修补”应用程序的代码:

private void Patch()
{
        using (WebClient client = new WebClient())
        {
            string name = "asd681648.txt";
            client.DownloadFile("http://mypage/patches/" + "Graph" + ".txt", @"C:\" + name);
            string text = File.ReadAllText(@"C:\" + name);
            File.Delete(@"C:\" + name);
            string[] version = text.Split('.');
            string[] sVersion = set.ver.Split('.');
            for (int i = 0; i < version.Length; i++)
            {
                if (int.Parse(version[i]) > int.Parse(sVersion[i]))
                {
                    MessageBox.Show("New version will be downloaded!");
                    Process.Start(Environment.CurrentDirectory + "\\Patcher.exe", "Graph "+set.ver+" "+Environment.CurrentDirectory+"\\");
                    Application.Exit();
                    break;
                }
            }
        }
}

以及“修补程序”的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Deployment.Compression.Cab;

namespace Patcher
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClient client=new WebClient();
            string name = "asd2341648.txt";
            client.DownloadFile("http://mypage/patches/"+args[0]+".txt", @"C:\"+name);
            string text = File.ReadAllText(@"C:\" + name);
            File.Delete(@"C:\"+name);
            string[] version = text.Split('.');
            string[] sVersion = args[1].Split('.');
            for (int i = 0; i < version.Length; i++)
            {
                if(int.Parse(version[i])>int.Parse(sVersion[i]))
                {
                    break;
                }
                if(i==version.Length-1)
                {
                    return;
                }
            }
            Console.WriteLine("New version is avaliable. Downloading...");
            client.DownloadFile("http://mypage/patches/patches/" + args[0]+".cab",       @"C:\"+args[0]+".cab");
            CabInfo cab=new CabInfo(@"C:\"+args[0]+".cab");
            cab.Unpack(args[2]);
            File.Delete( @"C:\"+args[0]+".cab");
        }
    }
}
4

1 回答 1

1

您可以使用(从您的第一个参数中删除 txt 扩展名)进行修改

var path = Path.Combine("C:\",name);
client.DownloadFile("http://mypage/patches/"+args[0], @path);

你也可以修改

 var path = Path.Combine("C:\",name);
 client.DownloadFile("http://mypage/patches/", @path);

您必须只设置文件的地址

public void DownloadFile(
    string address,
    string fileName
)

链接:http: //msdn.microsoft.com/fr-fr/library/vstudio/ez801hhe.aspx

于 2012-09-25T16:22:48.297 回答