首页 > 解决方案 > c#WebClient 将文件从 url 下载到所需目录不(真的)工作

问题描述

我是 C# 和 Visual Studio Windows 窗体的新手。我试图制作一个安装程序只是为了看看 c# 是如何工作的,但我有一个小问题。使用我的“程序”,您首先选择要下载文件的位置,然后单击下载按钮进行下载。所以我选择了我想要文件的位置,但它做了一些奇怪的事情。所以你选择文件夹并下载文件,但它会下载带有文本 CHROMEtest.txt 的文件(称为 test.txt)(我选择 chrome 作为我的安装目录)我的 chrome 文件夹在我的桌面文件夹中,所以我选择 chrome作为我的下载文件夹,但随后它作为 CHROMEtest.txt 安装在我的桌面上。这可能是因为我使用 textBox1.Text 作为目录。我将在这里发布代码以供理解。

字符串:

        string url1 = "https://www.dropbox.com/s/f8f3x5qgmkeqis6/test.txt?dl=1";
        string fileName1 = "text.txt";```

code:

oldBrowserDialog fbd = new FolderBrowserDialog();

    private void folderbrowser_Click_1(object sender, EventArgs e)
    {


        if (fbd.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = fbd.SelectedPath;
        }
    }
    private void Install_Button_Click_1(object sender, EventArgs e)
    {


        if (textBox1.Text.Length > 0)
        {
            //Downloading file form Http Url
            WebClient myWebClient = new WebClient();
            myWebClient.DownloadFile(url1, textBox1.Text + fileName1);
            //CompleteDownloading file form Http Url
        }
        else
        {
            MessageBox.Show("File path not found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

        }


    }
i hope you guys can help me :D

标签: c#winformswebclient

解决方案


推荐阅读