winforms C#使用(WebClient)将文件下载到特定路径不起作用

hvvq6cgz  于 2022-11-17  发布在  C#
关注(0)|答案(1)|浏览(183)

我现在正在学习C#,我正在开发一种工具,用户可以下载某些文件。但是,我希望下载的文件最终位于Downloads文件夹中,而不是“bin\Realse”中。
路径也是在以下之前创建的:[(系统.IO.目录.创建目录(“C:\用户\”+环境.用户名.ToString()+“\下载\Glebi工具\游戏”);)]

代码:

private void btnMinecraft_Click(object sender, EventArgs e)
    {
        wc.DownloadFileCompleted += new AsyncCompletedEventHandler(FileDownloadComplete1);
        Uri rarurl = new Uri("https://cdn.discordapp.com/attachments/1016411808887746570/1016422145229848686/MfW10_Fix_Repair_UWP_Generic.rar");
        wc.DownloadFileAsync(rarurl, "MfW10_Fix_Repair_UWP_Generic.rar", @"C:\\Users\\" + Environment.UserName() + "\\Downloads\\Glebi-Tool\\Games");
    }
mv1qrgav

mv1qrgav1#

所以我自己设法弄明白了。答案是:

private void btnMinecraft_Click(object sender, EventArgs e)
    {

        if (System.IO.File.Exists(@"C:\\Users\\" + Environment.UserName + "\\Downloads\\Glebi-Tool\\Games\\MfW10_Fix_Repair_UWP_Generic.rar"))
        {
            MessageBox.Show("Already Downlaoded");
        }
        else
        {
            {
                using (var wc = new WebClient())

                    wc.DownloadFile("https://cdn.discordapp.com/attachments/1016411808887746570/1016422145229848686/MfW10_Fix_Repair_UWP_Generic.rar", "MfW10_Fix_Repair_UWP_Generic.rar");
            }

            string fromPath = Path.Combine(Application.StartupPath, "MfW10_Fix_Repair_UWP_Generic.rar");
            string toPath = Path.Combine(@"C:\\Users\\" + Environment.UserName + "\\Downloads\\Glebi-Tool\\Games", "MfW10_Fix_Repair_UWP_Generic.rar");

            // Move the file.
            File.Move(fromPath, toPath);

            MessageBox.Show("Download Completed");
        }
    }

相关问题