【问题标题】:How do I download and unzip a program in C #如何在 C# 中下载和解压缩程序
【发布时间】:2021-07-19 00:35:53
【问题描述】:

我正在为游戏服务器制作启动器。如何让游戏下载并解压缩。我准备了一个 zip 文件,我希望它自动下载并安装“C:\Program Files”。

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {

        WebClient web = new WebClient();

        public Form1()
        {
            InitializeComponent();
        }

        private void DosyaIndirmeTamamlandiUyarisi(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Download Completed");
        }

        private void Download_Click(object sender, EventArgs e)
        {
            web.DownloadFileCompleted += new AsyncCompletedEventHandler(DosyaIndirmeTamamlandiUyarisi);

            Uri DosyaAdresi = new Uri(textBox1.Text);
            web.DownloadFileAsync(DosyaAdresi, textBox1.Text.Length.ToString()+".zip");
        }
    }
}

【问题讨论】:

  • 您在为哪一部分苦苦挣扎?看起来您设法下载了该文件。第二个任务见How to: Compress and extract files。另外,请注意,为了访问 %ProgramFiles% 文件夹,您需要提升您的进程(AKA,以管理员身份运行)。
  • 我试过这样,但它没有解压缩。 pastecode.io/s/1hija2BtUu

标签: c# winforms


【解决方案1】:

https://docs.microsoft.com/en-ca/dotnet/standard/io/how-to-compress-and-extract-files

解压缩文件。 将 async 和 await 添加到您的代码中。

更改为您自己的设置。

public partial class Form1 : Form
    {
        private WebClient _webClient;
        public Form1()
        {
            InitializeComponent();
            _webClient = new WebClient();
        }

        private async void Download_Click(object sender, EventArgs e)
        {
            Uri DosyaAdresi = new Uri(@"https://zaycev.net/download/packie/ae4cce5699f15a877ff42559b5a2555d/9443");
            var fileName = "download";
            await _webClient.DownloadFileTaskAsync(DosyaAdresi, fileName + ".zip");
            DirectoryInfo unZipDir = new DirectoryInfo("unzip");
            if (!unZipDir.Exists)
            {
                unZipDir.Create();
            }
            ZipFile.ExtractToDirectory(fileName + ".zip", unZipDir.FullName);
        }

【讨论】:

  • 我试过这样,但它没有解压缩。 pastecode.io/s/1hija2BtUu
  • @YavuzAdem 解压缩到一个文件夹。我更新了答案
  • @DirectoryInfo unZipDir = new DirectoryInfo("unzip");指定您的目录,而不是解压缩。
猜你喜欢
  • 1970-01-01
  • 2011-07-06
  • 2010-09-06
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多