远程文件到变量(C#)

时间:2016-06-11 09:46:44

标签: c# visual-studio-2012

我正在尝试为旧Toontown Online使用的某种类型的文件创建一个阅读器。它是patcher.ver。我已经尝试从托管的页面读取变量和字典,但似乎无法让它工作!

WebRequest request = WebRequest.Create(Form1.patcherBaseURL + "/patcher.ver");
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
    html = sr.ReadToEnd();
    var dic = File.ReadAllLines(html)
        .Select(l => l.Split(new[] { '=' }))
        .ToDictionary(s => s[0].Trim(), s => s[1].Trim());
    string PatchVerStrServer = dic["PATCHER_VERSION_STRING_SERVER"];

这是我试图使用的代码。但是我不断获得例外"illegal characters in path"

我想要阅读的文件位于:http://dolimg.com/toontown/uk/patcher.ver

谢谢,如果你能提供帮助!我是C#的新手,我似乎无法找到与此相关的任何内容。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码。 html中有一些注释掉的行,所以我用了一个跳过这些行的地方

WebRequest request = WebRequest.Create(Form1.patcherBaseURL + "/patcher.ver");
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
    html = sr.ReadToEnd();
    var dic = html
        .Split (new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
        .Where (line => !line.StartsWith("#"))        // Skip commented out lines
        .Select(line => line.Split(new[] { '=' }, 2)) // Beware: some values have spaces
        .ToDictionary(token => token[0].Trim(), token => token[1].Trim());

    string patchVerStrServer = dic["PATCHER_VERSION_STRING_SERVER"];
}