简体   繁体   English

C#未下载

[英]C# not downloading

I have problem with downloading files through C#. 我在通过C#下载文件时遇到问题。

I have a class which handles downloading like this: 我有一个可以像这样处理下载的类:

namespace Ultra_Script
{
    class FileDownloader
    {

        private readonly string _url;
        private readonly string _fullPathWheretoSave;
        private bool _result = false;
        private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);

        public FileDownloader(string url, string fullPathWheretoSave)
        {

            if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
            if (string.IsNullOrEmpty(fullPathWheretoSave)) throw new ArgumentNullException("fullPathWhereToSave");

            this._url = url;
            this._fullPathWheretoSave = fullPathWheretoSave;

        }

        public bool StartDownload(int timeout)
        {

            try
            {
                System.IO.Directory.CreateDirectory(Path.GetDirectoryName(_fullPathWheretoSave));

                if (File.Exists(_fullPathWheretoSave))
                {
                    File.Delete(_fullPathWheretoSave);
                }
                using (WebClient client = new WebClient())
                {
                    var ur = new Uri(_url);
                    //client.Credentials = new NetworkCredential("username", "password");
                    client.DownloadProgressChanged += WebClientDownloadProgressChanged;
                    client.DownloadFileCompleted += WebClientDownloadCompleted;
                    Console.WriteLine(@"Downloading File:");
                    client.DownloadFileAsync(ur, _fullPathWheretoSave);
                    _semaphore.Wait(timeout);
                    return _result && File.Exists(_fullPathWheretoSave);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Cant download file");
                Console.Write(e);
                return false;
            }
            finally
            {
                this._semaphore.Dispose();
            }

        }

        private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Console.Write("/r   -->    {0}%", e.ProgressPercentage);
        }

        private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)
        {
            _result = !args.Cancelled;
            if (!_result)
            {
                Console.Write(args.Error.ToString());
            }
            Console.WriteLine(Environment.NewLine + "Download Finished!");
            _semaphore.Release();
        }

        public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)
        {
            return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);
        }

    }
}

And i called it like this: 我这样称呼它:

public static void InstallBasicSW()
        {

            var succes = FileDownloader.DownloadFile("https://github.com/Corbieman/Basic_SW/raw/master/JaVa.exe", "C:\\Windows", 99999999);
            Console.WriteLine("Done - Succes: " + succes);
            Console.ReadLine();

        }

But only what im getting in Console is: 但是,只有我在Console中获得的是:

Download Finished! 下载完成!

Done - Succes: False; 完成-成功:错误;

I don't get any error message or progress bar. 我没有收到任何错误消息或进度条。 This message just pops up instantly. 这则消息会立即弹出。 And file doesn't download into that path. 文件不会下载到该路径。 Anybody know or have idea where can be the problem? 有人知道或知道哪里可能出问题吗?

The parameter of method DownloadFile need the full file's path. 方法DownloadFile的参数需要完整文件的路径。

Try this : 尝试这个 :

    public static void InstallBasicSW()
    {
        var succes = FileDownloader.DownloadFile("https://github.com/Corbieman/Basic_SW/raw/master/JaVa.exe", @"C:\Temps\JaVa.exe", 99999999);
        Console.WriteLine("Done - Succes: " + succes);
        Console.ReadLine();
    }

Why have you this result? 为什么会有这个结果? I think it's because you pass a directory path instead of a file path. 我认为这是因为您传递的是目录路径而不是文件路径。 The download cancel and finish immediately. 下载取消并立即完成。

A explicit exception will be more helpful... 明确的例外会更有帮助...

So problem was i had to run it as admin and correct the path like you said guys. 所以问题是我不得不像管理员一样以管理员身份运行它并更正路径。 Its working now, Thanks. 现在工作了,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM