Argument exception on URI parsing when downloading video

219 Views Asked by At

I'm not experienced with C#, but I got the basics down. Now I'm trying to download videos from YouTube with the Video Library (in the VS package manager: Install-Package VideoLibrary).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.IO;
using VideoLibrary;

namespace TubeDemo
{
    public partial class MainWindow : Window
    {
        string link = "https://www.youtube.com/watch?v=8SbUC-UaAxE";
        string link2 = "https://www.youtube.com/watch?v=BlRqTNkgEuo";
        public MainWindow()
        {
            InitializeComponent();
        }
        void SaveVideoToDisk_Click(object sender, EventArgs e)
        {
            var youTube = YouTube.Default; // starting point for YouTube actions
            var video = youTube.GetVideo(link2); // gets a Video object with info about the video
            File.WriteAllBytes(@"C:\testfire\" + video.FullName, video.GetBytes());
        }
    }
}

Above functions SaveVideoToDisk_Click gets called from a .xaml button, which works fine. But not every video works OK. video.URI gets awfully big, over 800 characters. Some of the URLs turn out to cause the video.URI to throw an exception:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

In the code provided, passing link as argument throws while passing link2 works just fine.

  1. Can I fix this?
  2. If not, how should I handle these exceptions? Just try, catch and report or is checking before a better idea?
0

There are 0 best solutions below