-2

我有一个 Winforms 自动更新程序。

如果我打开程序,它将从包含 .zip 文件下载链接的原始文本中获取文件下载链接,该链接来自网络。

但是,如果该人创建了一个文件夹并将程序放在文件夹中,我希望程序获取程序所在的文件夹名称并将 zip 文件解压缩到该文件夹​​中。

这是我的代码:

private void StartDownload()
        {
            WebClient webClient = new WebClient();
            string path = "./Sympathy";
            string address1 = "https://ghostbin.co/paste/cjx7j/raw";
            string address2 = webClient.DownloadString(address1);
            this.progressBar1.Value = 100;
            string str = "./Sympathy.zip";
            if (System.IO.File.Exists(str))
            {
                System.IO.File.Delete(str);
            }
            else
            {
                webClient.DownloadFile(address2, str);
                ZipFile.ExtractToDirectory(str, Directory.GetCurrentDirectory(Directory).Name); 
                System.IO.File.Delete(str);
            }
        }

但是我有一个错误,我该(Directory)如何解决?

4

3 回答 3

1

您收到的错误是什么?这将有助于解决您遇到的问题。

您提到您(Directory)的代码部分出现错误,我在这一行看到了一些内容:

  1. 该方法Directory.GetCurrentDirectory不需要输入;
  2. 您不能将类型作为参数传递;
  3. 调用Directory.GetCurrentDirectory()返回一个字符串,你不能调用这个属性Name

请注意,此调用的结果并不总是您的应用程序所在的位置。如果您检查超链接的属性,您可以设置Target' 和Start in. 这Target是您的程序Start in,是提供给您的应用程序的当前目录。除了创建超链接,您还可以打开命令提示符(默认为您的主目录)并键入应用程序的完整路径,这将导致您的应用程序打开并接收命令提示符指向的路径作为当前目录(在默认情况下,您的主目录)。帖子获取当前文件夹路径中的答案对此进行了更详细的介绍。

于 2020-08-01T10:41:23.667 回答
1

要获取可执行文件的路径,请使用System.Reflection.Assembly.GetEntryAssembly().Location;获取目录Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

我看到你的代码有缺陷。改用它来做所有事情:

string archivePath = "Sympathy.zip";
using (var client = new WebClient())
{
    client.DownloadFile(client.DownloadString("https://ghostbin.co/paste/cjx7j/raw"), archivePath);
    ZipFile.ExtractToDirectory(archivePath, Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location));
    File.Delete(archivePath);
}

有用。它下载了一堆未知文件。

于 2020-08-01T10:42:11.223 回答
0

使用此行,您可以获得应用程序的当前目录。

Application.StartupPath;

它为您提供 .exe 运行应用程序的文件夹。

于 2020-08-01T09:04:43.143 回答