c# - WebClient 抛出 "No Data Available for Encoding 1252"

标签 c# visual-studio-2010 encoding webclient windows-1252

我正在制作一个运行良好的程序,只是我开始创建一个更新系统,但不知何故它无法运行。初始屏幕使用 webClient 检查更新,现在似乎也出错了。我将其设置为启动画面下载一个文本文件。如果版本不匹配,将启动一个单独的表单并使用 webClient 下载更新程序。我已经两次创建此表单。它有 1 个进度条、1 个 webClient、一个图像背景和 1 个标签。应该下载更新,在栏上显示其进度,然后打开更新程序并关闭主程序。我第一次创建表单时运行它,它会卡住并且不会下载文件。停止调试后,visual c# express 2010 的设计窗口会崩溃,我不得不重新启动 vc#。该窗口将显示我添加的所有项目,但 webClient 除外。所以我再次尝试调试并收到此消息:

Error   1   Invalid Resx file. Type  in the data at line 138, position 5, cannot be loaded because it threw the following exception during construction: No data is available for encoding 1252.    C:\Users\DjLyriz\documents\visual studio 2010\Projects\TubeRip\TubeRip\updates.resx TubeRip

Error   2   TargetInvocationException: Type  in the data at line 138, position 5, cannot be loaded because it threw the following exception during construction: No data is available for encoding 1252.
at System.Resources.ResXResourceReader.ParseXml(XmlTextReader reader)
at System.Resources.ResXResourceReader.EnsureResData()
at System.Resources.ResXResourceReader.GetEnumerator()
at Microsoft.Build.Tasks.ProcessResourceFiles.ReadResources(ReaderInfo readerInfo, IResourceReader reader, String fileName)
at Microsoft.Build.Tasks.ProcessResourceFiles.ReadResources(String filename, Boolean shouldUseSourcePath)
at Microsoft.Build.Tasks.ProcessResourceFiles.ProcessFile(String inFile, String outFileOrDir)
XmlException: Type  in the data at line 138, position 5, cannot be loaded because it threw the following exception during construction: No data is available for encoding 1252. Line 138, position 5.

NotSupportedException: No data is available for encoding 1252.
at System.Text.BaseCodePageEncoding.LoadCodePageTables()
at System.Text.BaseCodePageEncoding..ctor(Int32 codepage, Int32 dataCodePage)
at System.Text.SBCSCodePageEncoding..ctor(SerializationInfo info, StreamingContext context)
C:\Users\DjLyriz\documents\visual studio 2010\Projects\TubeRip\TubeRip\updates.resx TubeRip

所以我从表单中删除了 webClient 并重试。我第二次遇到同样的错误。所以我用谷歌搜索了第一个错误,并在 hackforums 上找到了一个没有修复的响应。现在我完全迷路了。我所有的网络客户端现在似乎都在这样做,我不知道为什么。

这是我的启动画面中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace TubeRip
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start();
        getupdates();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer2.Start();
    }

    private void timer2_Tick_1(object sender, EventArgs e)
    {
        timer1.Stop();
        label1.Text = "Loading Core Components..." + " " + progressBar1.Value.ToString() + "%";

        if (progressBar1.Value < 20)
        {
            progressBar1.Value += 1;
        }
        else
        {
            timer3.Start();
        }
    }
    private void end()
    {
        timer7.Stop();
        mainpage home = new mainpage();
        home.Show();
        this.Dispose(false);
    }

    private void timer3_Tick(object sender, EventArgs e)
    {
        timer2.Stop();
        label1.Text = "Loading Encryption Algorithyms..." + " " + progressBar1.Value.ToString() + "%";
        if (progressBar1.Value < 40)
        {
            progressBar1.Value += 1;
        }
        else
        {
            timer4.Start();
        }
    }

    private void timer4_Tick(object sender, EventArgs e)
    {
        timer3.Stop();
        label1.Text = "Enabling Download Services..." + " " + progressBar1.Value.ToString() + "%";
        if (progressBar1.Value < 60)
        {
            progressBar1.Value += 1;
        }
        else
        {
            timer5.Start();
        }

    }

    private void timer5_Tick(object sender, EventArgs e)
    {
        timer4.Stop();
        label1.Text = "Disabling Youtube's Download Protection..." + " " + progressBar1.Value.ToString() + "%";
        if (progressBar1.Value < 80)
        {
            progressBar1.Value += 1;
        }
        else
        {
            timer6.Start();
        }

    }

    private void timer6_Tick(object sender, EventArgs e)
    {
        timer5.Stop();
        label1.Text = "Drawing GUI" + " " + progressBar1.Value.ToString() + "%";
        if (progressBar1.Value < 90)
        {
            progressBar1.Value += 1;
        }
        else
        {
            timer7.Start();
        }
    }

    private void timer7_Tick(object sender, EventArgs e)
    {
        timer6.Stop();
        label1.Text = "Creating The first Humanlike Robot..." + " " + progressBar1.Value.ToString() + "%";
        if (progressBar1.Value < 100)
        {
            progressBar1.Value += 1;
        }
        else
        {
            end();
        }
    }
    private void getupdates()
    {
        try
        {
            string updateurl = "http://dl.dropbox.com/u/22054429/TubeRip_version.txt";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(updateurl);
            WebResponse response = request.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("windows-1252"));
            string update = sr.ReadToEnd();
            int build = Convert.ToInt32(update);
            int thisbuild = 2;
            if (build > thisbuild)
            {
                label2.Visible = true;
                TubeRip.Properties.Settings.Default.UpdateAvail = true;
            }
            else
            {
                label2.Visible = false;
                TubeRip.Properties.Settings.Default.UpdateAvail = false;
            }
        }
        catch
        {
            MessageBox.Show("Unable to connect to update server! Please try again later.");
        }
    }

}
}

这是我的更新程序的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;

namespace TubeRip
{
public partial class updates : Form
{
    public updates()
    {
        InitializeComponent();
    }

    private void updates_Load(object sender, EventArgs e)
    {
        Uri update = new Uri("http://dl.dropbox.com/u/22054429/TubeRip_Installer.exe");
        webClient1.DownloadFileAsync(update, "update.exe");
        webClient1.DownloadProgressChanged +=new DownloadProgressChangedEventHandler( webClient1_DownloadProgressChanged);
        webClient1.DownloadFileCompleted += new AsyncCompletedEventHandler( webClient1_DownloadFileCompleted);
    }
    void  webClient1_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        Process.Start("update.exe");
        Environment.Exit(0);
    }
    void  webClient1_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        label1.Text = "Downloading Updates Please Wait... " + e.ProgressPercentage.ToString() + "%";
    }
}
}

最佳答案

我没有找到我的特定错误的任何结果,但我确实找到了解决它的方法。我没有使用 webClient 的默认编码,而是将自己的编码设置为 UTF-8,这不会引发此错误。修改后的代码最终起作用了。

WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;
Uri update = new Uri(uri);
client.DownloadFileAsync(update, "update.exe");
client.DownloadProgressChanged +=new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

否则,我找不到解决此问题的方法,也没有理由出现此问题。

关于c# - WebClient 抛出 "No Data Available for Encoding 1252",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11063080/

相关文章:

javascript - 如何使用jQuery来显示HtmlTableRow(开始隐藏/隐藏生命)?

javascript - AngularJS:不使用单页时有没有办法使用 $routeParams

带参数的 jQuery WCF 服务 MVC2 VS2010 .NET 4.0 调用失败

database - SSDT Unresolved 引用错误 SQL71561

php - mysql_real_escape_string 和撇号问题

php - 将字符串拆分为带有瑞典字符的单词

c# - 为什么不能在没有括号的内插字符串中使用条件运算符?

visual-studio-2010 - 从 Visual Studio 外部构建 Visual Studio 项目时如何 "get rid of"$(SolutionDir)

javascript - 将表情符号编码为 un​​icode 代码点 - PHP/JS

c# - 安装向导中的数据库连接