Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法下载包含视频的子网站_C#_Wpf_Youtube - Fatal编程技术网

C# 无法下载包含视频的子网站

C# 无法下载包含视频的子网站,c#,wpf,youtube,C#,Wpf,Youtube,我已经做了一个简单的程序(紧紧依靠示例)来可视化我的问题。它只从指定的URL下载一个文件,仅此而已;在我尝试使用这些数据之前,一切都正常: 令人惊讶的是,什么也没发生。为什么它既不下载相关视频也不下载HTML源代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; us

我已经做了一个简单的程序(紧紧依靠示例)来可视化我的问题。它只从指定的URL下载一个文件,仅此而已;在我尝试使用这些数据之前,一切都正常:

令人惊讶的是,什么也没发生。为什么它既不下载相关视频也不下载HTML源代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Net;
using System.IO;
using System.ComponentModel;

namespace downloader
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void DownloadButton_Click(object sender, RoutedEventArgs e)
        {
            WebClient client = new WebClient();

            string fileName;

            try
            {
                fileName = System.IO.Path.GetFileName(URLBox.Text);
                Uri currentURL = new Uri(URLBox.Text);

                client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

                client.DownloadFileAsync(currentURL, fileName);
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                progressBar.Value = 0;
            }

        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Download Completed!");


        }

        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;

        }


    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
Net系统;
使用System.IO;
使用系统组件模型;
命名空间下载程序
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有无效下载按钮\单击(对象发送者,路由目标e)
{
WebClient客户端=新的WebClient();
字符串文件名;
尝试
{
fileName=System.IO.Path.GetFileName(URLBox.Text);
Uri currentURL=新Uri(URLBox.Text);
client.DownloadFileCompleted+=新的AsyncCompletedEventHandler(已完成);
client.DownloadProgressChanged+=新的DownloadProgressChangedEventHandler(ProgressChanged);
client.DownloadFileAsync(当前URL,文件名);
}
捕获(异常exc)
{
MessageBox.Show(exc.Message);
progressBar.Value=0;
}
}
私有无效已完成(对象发送方,AsyncCompletedEventArgs e)
{
显示(“下载完成!”);
}
私有void ProgressChanged(对象发送方,下载progresschangedeventargs e)
{
progressBar.Value=e.ProgressPercentage;
}
}
}

对于这样的URL,这一行

fileName = System.IO.Path.GetFileName(URLBox.Text);

…将
fileName
设置为
“watch?v=pqaARDsiJv4”
,这不是一个合法的文件名。

我认为你不能从Youtube上下载这样的视频。我将向您介绍去年关于从Youtube下载内容的堆栈交换问题;这里有很多链接,甚至有一种方法。但所有这些解决方案都已经使用了3年,现在可能不再有效;这是你想做的部分问题;谷歌正在不断地做出改变,这很可能会打破任何现有的解决方案

您可以查看此处提供的任何解决方案是否仍然有效:
或者使用提供的信息创建您自己的。

@johnmithell在这种情况下,不会下载任何内容。这是因为它是一个YouTube链接。。。谷歌正在保护他们的知识产权……没有例外,这是多么大的疏忽啊!嗯,下载是在另一个线程上,这些线程往往会默默地失败。