2017-10-11 4 views
0

私はyoutubeからビデオファイルをダウンロードするプログラムを実装しようとしています。私は2つのファイルを持つこの小さなWindowsコンソールアプリケーションを書いています。youtubeファイルをダウンロードするC#アプリはUnauthorizedAccessExceptionを返します

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); 
     }  
    } 
} 

Program.csの

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

namespace AsyncAwait 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Downloader d = new Downloader(); 
      d.DownloadFilesAsync().Wait(); 
      Console.WriteLine("finished");    
     } 
    } 
} 

私はここで、次のエラーを取得しています。

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 

なぜこのエラーが発生しますか。私はlibvideoを使ってyoutubeのビデオをダウンロードしています

+3

アクセスをしようとしているが、Cのルートフォルダにファイルを保存しないようにしてください:, Windowsの最新バージョンはそれから保護されています。 –

答えて

1

このディレクトリを作成しましたか?

「C:PythonのイントロP.1と\実用的な機械学習のチュートリアル - YouTube.mp4」

フレームワークは、このパス

関連する問題