天气预报

来源:互联网 发布:sftp和ssh端口分离 编辑:程序博客网 时间:2024/04/29 02:49

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Net;
using System.IO;
using System.Threading;
namespace startCom
{
public delegate void myDelegate();
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.ComponentModel.IContainer components = null;

public event myDelegate myEventA;
public event myDelegate myEventB;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox1.Size = new System.Drawing.Size(504, 496);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(504, 493);
this.Controls.Add(this.textBox1);
this.MinimumSize = new System.Drawing.Size(512, 520);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "服务器数据处理";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
//timer1.Start();
//timer1.Interval = 1000;//86400000;
//timer1.Enabled = true;
////启动定时器
//timer1.Start();
//timer1.Tick += new EventHandler(doget);//时间到响应方法aa()
//timer1.Tick += new EventHandler(deltemp);
////timer1.Interval = 200;// 则是设定时间间隔,到了响应方法
////timer1.Stop(); //停止

//MessageBox.Show(DateTime.Now.Hour.ToString());

//button1.Enabled = false;

Thread thd = new Thread(new ThreadStart(doEvent));
thd.IsBackground = true;
thd.Start();
//thd.Abort();

//Thread.Sleep(new TimeSpan(100000));
//Thread.Sleep(100000);

}
private void doget()
{
string connStr = "机密去掉:)";

SqlConnection conn = new SqlConnection(connStr);
SqlCommand comm ;
comm =new SqlCommand("select cityname from city",conn);
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
textBox1.Text = "天气预报开始:"+System.DateTime.Now.ToString("yyyyMMdd")+"/r/n";
while(dr.Read())
{
string ct = dr["cityname"].ToString();
textBox1.Text += ct +"/r/n";
try
{
string tmp = GetContentFromUrll("http://www.cma.gov.cn/netcenter_news/qxyb/city/index.php?city="+ System.Web.HttpUtility.UrlEncode(ct,System.Text.Encoding.GetEncoding("gb2312")));
//Response.Write(("成都"));
tmp = tmp.Substring(tmp.IndexOf("<!--未来天气预报开始-->"),tmp.IndexOf("<!--指数预报结束-->")-tmp.IndexOf("<!--未来天气预报开始-->"));
string dt = getString(tmp,1) + "|" + getString(tmp,2) +"|"+getString(tmp,3);
string wt = getString(tmp,5) + "|" + getString(tmp,6) +"|"+getString(tmp,7);
string wd = getString(tmp,9) + "|" + getString(tmp,10) +"|"+getString(tmp,11);
string wl = getString(tmp,13) + "|" + getString(tmp,14) +"|"+getString(tmp,15);
string wen = getString(tmp,17) + "|" + getString(tmp,18) +"|"+getString(tmp,19);
string wu = "";
if(tmp.IndexOf("污染指数") != -1)
{
wu = "污染指数:"+getString(tmp,21) + "<br/>/n"+"紫外线指数:"+getString(tmp,23)+"<br/>/n"+"舒适度指数:"+getString(tmp,25)+"<br/>/n"+"穿衣指数:"+getString(tmp,27);
}
//string sqlCmd = "insert into weather (city,[date],weather,windl,wind,wendu,wuran) values('"+ ct +"','"+ dt +"','"+ wt +"','"+ wd +"','"+ wl +"','"+ wen+"','"+ wu +"')";

string sqlCmd = "update weather set [date]='"+ dt +"',weather='"+wt+"',windl='"+ wd +"',wind='"+ wl +"',wendu='"+ wen +"',wuran='"+ wu +"' where city='"+ ct +"'";
SqlConnection con = new SqlConnection(connStr);
con.Open();
SqlCommand cmd = new SqlCommand(sqlCmd,con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
catch(System.Exception er)
{
textBox1.Text += er+"/r/n";
//Application.Exit();
}
//Application.DoEvents();
}
textBox1.Text += "天气预报结束/r/n";
dr.Close();
conn.Close();
comm.Dispose();
conn.Dispose();
}
private void deltemp()
{
string path = "d://wap//tmp//";
DirectoryInfo DInfo=new DirectoryInfo(path);
//string sum="";
try
{
foreach(FileInfo NextFile in DInfo.GetFiles())
{
File.Delete(path+NextFile.Name);
}
Application.DoEvents();
textBox1.Text += "Tmp文件删除成功!"+System.DateTime.Now.ToString("yyyyMMdd")+"/r/n";;
}
catch(Exception er)
{
textBox1.Text += "文件删除有误!"+er.ToString();
}

}
private string getString(string tmp,int i)
{
string temp = tmp.ToLower();
int _index = temp.IndexOf("<font");
for(int n = 1 ; n < i ; n++)
{
_index = temp.IndexOf("</font>",_index,temp.Length-_index-1)+7;
}
//Response.Write(_index);
temp = temp.Substring(_index,temp.IndexOf("</font>",_index,temp.Length-_index-1)-_index);
return System.Text.RegularExpressions.Regex.Replace(System.Text.RegularExpressions.Regex.Replace(temp,"<(.|[/f/n/r/t/v])+?>|[/n/r/t]","",System.Text.RegularExpressions.RegexOptions.IgnoreCase),"[(&nbsp;)|( )]+","&nbsp;",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}
private string GetContentFromUrll(string _requestUrl)
{
string _StrResponse ="";
HttpWebRequest _WebRequest = ( HttpWebRequest )WebRequest.Create( _requestUrl );
_WebRequest.Method = "GET";
WebResponse _WebResponse = _WebRequest.GetResponse();
StreamReader _ResponseStream = new StreamReader( _WebResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
_StrResponse = _ResponseStream.ReadToEnd();
_WebResponse.Close();
_ResponseStream.Close();
return _StrResponse;
}


void doEvent()
{
//int weather = int.Parse(weatherTime.Text);
//int del = int.Parse(fileTime.Text);
//if(weather < 1 || weather > 24 || del < 1 || del > 24)
//{
//MessageBox.Show("时间输入有错!");
//button1.Enabled = true;
//return ;
//}
while(true)
{

DateTime now = DateTime.Now;
if(now.Hour == 9)
{
myEventA = new myDelegate(doget);
}
if(now.Hour == 3)
{
myEventB = new myDelegate(deltemp);
}
if(myEventA != null) myEventA();
if(myEventB != null) myEventB();
Application.DoEvents();
Thread.Sleep(600000);
}
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Application.Exit();
}
}
}
 

原创粉丝点击