【问题标题】:how to fix Webexception unhandle如何修复未处理的 Web 异常
【发布时间】:2013-12-13 12:56:04
【问题描述】:

我想制作控制台 c# 程序以从 web 下载和执行 program.exe 文件。 所以当我运行这个程序时出现“Webexception unhandle”错误。任何人都可以帮助我。谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Diagnostics;
using System.IO;

namespace Downloader{
class Program
{
    static void Main(string[] args)
    {
        Uri uri = new Uri("http://www.pendrivelinux.com/downloads/Universal-USB-Installer/Universal-USB-Installer-1.9.5.1.exe");
        string filename = @"C:\bootable.exe";

        using (var wc = new WebClient())
        {
            wc.Credentials = CredentialCache.DefaultCredentials;
            wc.Headers.Add(HttpRequestHeader.UserAgent, "anything");
            wc.DownloadFile(uri, filename);

        }
    }
}

}

谁能帮帮我。

【问题讨论】:

  • 显示你得到的错误。它可能包含很多关于为什么您的下载不起作用的提示。

标签: c# exception-handling


【解决方案1】:

我猜您的 WebException 出现在最后一行(请在以后将其包含在您的问题中 - 我们不能真的根据猜测为您提供帮助)。

wc.DownloadFile(uri, filename);

从此方法调用上的documentation,如果满足以下条件之一,它将抛出WebException

  1. BaseAddress 和地址组合形成的 URI 无效。
  2. 文件名为空或为空。
  3. 文件不存在。
  4. 下载数据时出错。

我们可以立即排除 #2,因为您已设置。要找出您遇到了哪种情况,请尝试查看异常的消息属性(如果有,则查看内部异常的消息属性)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多