下载youtube文件的C#应用程序出现未经授权的访问例外

下载youtube文件的C#应用程序出现未经授权的访问例外,c#,youtube,C#,Youtube,我正在尝试实现一个从youtube下载视频文件的程序。我已经编写了这个小的windows控制台应用程序,它有两个文件 Downloader.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using VideoLibrary; using System.IO;

我正在尝试实现一个从youtube下载视频文件的程序。我已经编写了这个小的windows控制台应用程序,它有两个文件

Downloader.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using VideoLibrary;
using System.IO;

namespace AsyncAwait
{
    public class Downloader
    {

        public Task DownloadFilesAsync()
        {
            // In the Real World, we would actually do something...
            // For this example, we're just going to print file 0, file 1.
            //this will execute the function in a strictly sequential manner.

            //await DownloadFile0();
            //await DownloadFile1();

            //var task1 = DownloadFile0();
            //var task2 = DownloadFile1();
            //return Task.WhenAll(task1, task2);

            //var tasks = new Func<Task>[]
            //{
            //     () => DownloadFile(1),
            //     () => DownloadFile(2),
            //     () => DownloadFile(3),
            //     () => DownloadFile(4),
            //     () => DownloadFile(5)
            //};
            //return Task.WhenAll(tasks.Select(task => task()).ToArray());

            var tasks2 = new Func<Task>[]
            {
                 () => DownloadYoutubeVideo("https://www.youtube.com/watch?v=OGxgnH8y2NM"),
                 () => DownloadYoutubeVideo("https://www.youtube.com/watch?v=FNQxxpM1yOs"),
            };
            return Task.WhenAll(tasks2.Select(task => task()).ToArray());
        }

        private void SaveVideoToDisk(string link)
        {
            var youTube = YouTube.Default; // starting point for YouTube actions
            var video = youTube.GetVideo(link); // gets a Video object with info about the video
            File.WriteAllBytes(@"C:\" + video.FullName, video.GetBytes());
        }

        public async Task DownloadYoutubeVideo(string link)
        {
            await Task.Run(() => SaveVideoToDisk(link));
        }

        public async Task DownloadFile(int TaskNumber)
        {
            int count = 0;
            while (count < 100)
            {
                Console.WriteLine("Downloading File {0,2} ----> {1,3}%", TaskNumber, count);
                await Task.Delay(100);
                count++;
            }
            await Task.Delay(100);
        }      
    }
}
我在这里得到以下错误

Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.UnauthorizedAccessException: Access to the path 'C:\Practical Machine Learning Tutorial with Python Intro p.1 - YouTube.mp4' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.File.InternalWriteAllBytes(String path, Byte[] bytes, Boolean checkHost)
   at System.IO.File.WriteAllBytes(String path, Byte[] bytes)
   at AsyncAwait.Downloader.SaveVideoToDisk(String link) in C:\Software\csharppen\AsyncAwait\AsyncAwait\Downloader.cs:line 50
   at AsyncAwait.Downloader.<>c__DisplayClass2_0.<DownloadYoutubeVideo>b__0() in C:\Software\csharppen\AsyncAwait\AsyncAwait\Downloader.cs:line 55
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at AsyncAwait.Downloader.<DownloadYoutubeVideo>d__2.MoveNext() in C:\Software\csharppen\AsyncAwait\AsyncAwait\Downloader.cs:line 55
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at AsyncAwait.Program.Main(String[] args) in C:\Software\csharppen\AsyncAwait\AsyncAwait\Program.cs:line 14
未处理的异常:System.AggregateException:发生一个或多个错误。-->System.UnauthorizedAccessException:访问路径“C:\Practical Machine Learning Tutorial with Python Intro p.1-YouTube.mp4”被拒绝。
在System.IO.\uuu Error.WinIOError(Int32 errorCode,字符串maybeFullPath)
在System.IO.FileStream.Init(字符串路径、文件模式、文件访问权限、Int32权限、布尔用户权限、文件共享、Int32缓冲大小、文件选项选项、安全属性secAttrs、字符串msgPath、布尔bFromProxy、布尔useLongPath、布尔checkHost)
位于System.IO.FileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享、Int32 bufferSize、文件选项选项、字符串msgPath、布尔bFromProxy、布尔useLongPath、布尔checkHost)
位于System.IO.File.InternalWriteAllBytes(字符串路径,字节[]字节,布尔校验主机)
在System.IO.File.WriteAllBytes处(字符串路径,字节[]字节)
在C:\Software\csharppen\asyncwait\asyncwait\Downloader.cs中的asyncwait.Downloader.SaveVideoToDisk(字符串链接)处:第50行
在c:\Software\csharppen\asyncwait\asyncwait\Downloader.c\uu\u显示c:\Software\csharppen\asyncwait\Downloader.cs中的Class2\u0.b\u0():第55行
在System.Threading.Tasks.Task.InnerInvoke()中
在System.Threading.Tasks.Task.Execute()中
---来自引发异常的上一个位置的堆栈结束跟踪---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter.GetResult()中
在C:\Software\csharppen\asyncwait\asyncwait\Downloader.d_u2.MoveNext()中的C:\Software\csharppen\asyncwait\asyncwait\Downloader.cs:第55行
---内部异常堆栈跟踪的结束---
位于System.Threading.Tasks.Task.ThrowifeException(布尔值IncludeTaskCanceledException)
在System.Threading.Tasks.Task.Wait(Int32毫秒计时,CancellationToken CancellationToken)
在System.Threading.Tasks.Task.Wait()中
在C:\Software\csharppen\asyncwait\asyncwait\Program.cs中的asyncwait.Program.Main(字符串[]args)处:第14行

为什么会发生这种错误。我正在使用下载youtube视频

您创建了此目录吗

'C:\Python Intro p.1实用机器学习教程-YouTube.mp4'


框架正在尝试访问此路径

UnauthorizedAccessException
表示流签名未正确解密。考虑使用一个更为新近的替代品。

尽量不要将文件保存到C的根文件夹中:Windows的最新版本不受此保护。
Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.UnauthorizedAccessException: Access to the path 'C:\Practical Machine Learning Tutorial with Python Intro p.1 - YouTube.mp4' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.File.InternalWriteAllBytes(String path, Byte[] bytes, Boolean checkHost)
   at System.IO.File.WriteAllBytes(String path, Byte[] bytes)
   at AsyncAwait.Downloader.SaveVideoToDisk(String link) in C:\Software\csharppen\AsyncAwait\AsyncAwait\Downloader.cs:line 50
   at AsyncAwait.Downloader.<>c__DisplayClass2_0.<DownloadYoutubeVideo>b__0() in C:\Software\csharppen\AsyncAwait\AsyncAwait\Downloader.cs:line 55
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at AsyncAwait.Downloader.<DownloadYoutubeVideo>d__2.MoveNext() in C:\Software\csharppen\AsyncAwait\AsyncAwait\Downloader.cs:line 55
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at AsyncAwait.Program.Main(String[] args) in C:\Software\csharppen\AsyncAwait\AsyncAwait\Program.cs:line 14