我正在尝试使用外部html文档中的streamreader:
WebRequest request = WebRequest.Create("http://example.com/version.html");
WebResponse response = request.GetResponse();
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
double current = double.Parse(reader.ReadToEnd());
if (current >= 10.5)
{
button1.Enabled = false;
}
但它抛出异常(0xc0000005) 'Access violation'
html的内容:0.10.5
我做错了什么?
编辑:
根据建议将双倍更改为版本:
WebRequest request = WebRequest.Create("http://example.com/version.html");
WebResponse response = request.GetResponse();
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
Version current = Version.Parse(reader.ReadToEnd());
if (current >= 0.10.5)
{
button1.Enabled = false;
}
仍然是同样的例外:
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
The program '[6344] APP.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[6344] APP.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
答案 0 :(得分:2)
如果您要解析的数字是版本,也许您应该考虑System.Version
类,而不是double
。
在字符串上调用Version.Parse
。
它也可以按预期用于比较版本。
Version v1 = Version.Parse("0.10.5");
Version v2 = Version.Parse("0.11.2");
Console.WriteLine("v2 > v1 = {0}", v2 > v1); // Outputs True
Console.WriteLine("v2 < v1 = {0}", v2 < v1); // Outputs False
我不确定你的问题是什么。以下代码对我来说很合适(在LINQPad 4会话中)
void Main()
{
string url = "http://m1.beatpad.net/version.html";
var request = WebRequest.Create(url);
var response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
var data = reader.ReadToEnd();
var ver = Version.Parse(data);
Console.WriteLine("{0}", ver);
}