C#问题(网络操作)
想写一个程序,地址是一个网站的txt文件private bool a = true;
private void Form1_Load(object sender, EventArgs e)
{
//private bool isExceed = true;
//string a = label1.Text;
string a = label1.Text;
label1.Text= new System.Net.WebClient().DownloadString("http://");
if (a=0)
{
}获取后,
若值为0,弹出“正常“
若为1,下载https://down.52pojie.cn/Tools/NET/dnSpy.zip至D:/
若为2,运行D:/ip.exe
if(a=0)一直报错
求大佬帮忙
以上标红部分也麻烦大佬解答
本帖最后由 百元大户 于 2020-11-20 20:33 编辑
写了一下
private static void WhatYouWant(string a)
{
switch (a)
{
case "0":
//MessageBox.Show("正常");
Console.WriteLine("正常");
break;
case "1":
if (HttpDownload("https://down.52pojie.cn/Tools/NET/dnSpy.zip", @"D:\dnSpy.zip"))
Console.WriteLine("下载完成");
break;
case "2":
Process.Start(@"D:\ip.exe");
break;
}
}
private static bool HttpDownload(string url, string path)
{
try
{
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using (Stream responseStream = response.GetResponseStream())
{
byte[] bArr = new byte;
int size = responseStream.Read(bArr, 0, bArr.Length);
while (size > 0)
{
fs.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, bArr.Length);
}
return true;
}
}
}
catch (Exception)
{
return false;
}
}
力度可以再大一些 本帖最后由 gfxy888 于 2020-11-20 18:54 编辑
应该说if(a==0)这样写的吧?
而且你怎么确定你获取的东西说0 空值应该说 空值(a=="")
首先,取回来的你需要知道是什么类型,没有a=0这种写法,这是赋值语句,并不是比较,==才是 我学过一点C#的基础语法,大概是这样一个问题
if这个是条件语法,是逻辑型的,只能用逻辑判断
你的这个写法可以换成 switch这个语法来弄一下
void Button5Click(object sender, EventArgs e)
{
textBox3.Text = new System.Net.WebClient().DownloadString("http://dianying.2345.com/list/-------1.html");
int a = Convert.ToInt32(textBox3.Text) ;
switch (a)
{
case 0:
MessageBox.Show("正常");
break;
case 1:
break;
case 2:
System.Diagnostics.Process.Start(@"F:\000\3.1.exe"); //直接
break;
}
}void Button5Click(object sender, EventArgs e)
{
textBox3.Text = new System.Net.WebClient().DownloadString("http://dianying.2345.com/list/-------1.html");
int a = Convert.ToInt32(textBox3.Text) ;
switch (a)
{
case 0:
MessageBox.Show("正常");
break;
case 1:
break;
case 2:
System.Diagnostics.Process.Start(@"F:\000\3.1.exe"); //直接
break;
}
}
== ”0“
这样就行了
报错就将错误信息贴出来啊……
没有错误信息就获取错误信息,打印出来,你不就知道错的原因了么 a 是 string 类型
if判断应该是 if (a.equals("0")) { //... } if("0"==a)
1、参考:https://www.cnblogs.com/louby/p/7121765.html
2、 Process process1 = new Process();
process1.StartInfo.FileName = ip.exe路径;
process1.Start();
一个=是赋值两个=才是判断值相同
页:
[1]
2