文章内容

2017/5/17 13:38:09,作 者: 黄兵

天气预报小程序

最近在GitHub看了一个查询天气预报的小程序,是波兰的一个人写的,具体地址:Pogoda

程序主要实现了以下功能:

  1. ping当前网络是否可达;
  2. 网页写入文件;
  3. 文件解析,之后显示指定内容;

下面是一些主要代码:

ping当前网络是否可达:

using System.Net.NetworkInformation;
using System.Windows.Forms;

namespace Pogoda
{
class Connection
{
public bool Internet()
{
try
{
var ping = new System.Net.NetworkInformation.Ping();

var result = ping.Send("www.baidu.com");

if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
return false;
else return true;

}
catch (PingException ad)
{
MessageBox.Show("Internet connection problem");
return false;
}
}
}
}

之后将网页的内容写入txt文件,代码如下:

using System;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace Pogoda
{
class GetHtml
{
private string html;
public void side(string woj, string miasto)
{
//pobieranie kodu html ze strony
try
{
string url = "http://www.twojapogoda.pl/polska/" + Replice(woj) + "/" + Replice(miasto);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
html = sr.ReadToEnd();
sr.Close();
}
catch (Exception ea)
{
MessageBox.Show(ea.ToString());
}
//Zapis do pliku w katalogu Pogoda

try
{
string projectPath = (Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName) + "/Resources/html.txt"; // path of project
System.IO.File.WriteAllText(projectPath, html);
}
catch (Exception er)
{
MessageBox.Show(er.ToString());
}
}

private string Replice(string str)
{
string input = str.Trim().ToLower();
string tmp = input.Replace("ą", "a");
tmp = tmp.Replace("ć", "c");
tmp = tmp.Replace("ę", "e");
tmp = tmp.Replace("ł", "l");
tmp = tmp.Replace("ń", "n");
tmp = tmp.Replace("ó", "o");
tmp = tmp.Replace("ś", "s");
tmp = tmp.Replace("ź", "z");
tmp = tmp.Replace("ż", "z");
string output = tmp.Replace(" ", "-");
return output;
}

}
}

之后文件解析,返回指定内容:

using System;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace Pogoda
{
class GetHtml
{
private string html;
public void side(string woj, string miasto)
{
//pobieranie kodu html ze strony
try
{
string url = "http://www.twojapogoda.pl/polska/" + Replice(woj) + "/" + Replice(miasto);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
html = sr.ReadToEnd();
sr.Close();
}
catch (Exception ea)
{
MessageBox.Show(ea.ToString());
}
//Zapis do pliku w katalogu Pogoda

try
{
string projectPath = (Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName) + "/Resources/html.txt"; // path of project
System.IO.File.WriteAllText(projectPath, html);
}
catch (Exception er)
{
MessageBox.Show(er.ToString());
}
}

private string Replice(string str)
{
string input = str.Trim().ToLower();
string tmp = input.Replace("ą", "a");
tmp = tmp.Replace("ć", "c");
tmp = tmp.Replace("ę", "e");
tmp = tmp.Replace("ł", "l");
tmp = tmp.Replace("ń", "n");
tmp = tmp.Replace("ó", "o");
tmp = tmp.Replace("ś", "s");
tmp = tmp.Replace("ź", "z");
tmp = tmp.Replace("ż", "z");
string output = tmp.Replace(" ", "-");
return output;
}

}
}

数据库查询,代码如下:

using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace Pogoda
{
class SQL
{
SqlConnection connection;
string connectionString;
private ComboBox boxState, boxCity;

public SQL(ComboBox boxState,ComboBox boxCity)
{
this.boxState = boxState;
this.boxCity = boxCity;
connectionString = ConfigurationManager.ConnectionStrings["Pogoda.Properties.Settings.dbWeatherConnectionString"].ConnectionString;
}

public void State()
{
using (connection = new SqlConnection(connectionString)) // dzieki using nie musimy zamknac connection using zrobi to za mnie, tak samo zobi z adapter
using (SqlDataAdapter adapter = new SqlDataAdapter("Select * from States", connection))
{
DataTable stateTable = new DataTable();
adapter.Fill(stateTable);

boxState.DisplayMember = "State";
boxState.ValueMember = "Id";
boxState.DataSource = stateTable;
boxState.DataSource = stateTable;
}
}


public void City()
{

string query = "Select a.City from Cities a " +
"Inner join StatesCity b on a.Id = b.CitiesId " +
"WHERE b.StatesId = @RecipeId";
using (connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(query, connection))
using (SqlDataAdapter adapter = new SqlDataAdapter(command))
{
command.Parameters.AddWithValue("@RecipeId", boxState.SelectedValue);


DataTable cityTable = new DataTable();
adapter.Fill(cityTable);

boxCity.DisplayMember = "City";
boxCity.ValueMember = "Id";
boxCity.DataSource = cityTable;
}

}



}
}

主要的代码就在这里了,如果一知半解可以到这里看详细的源代码。

项目地址:Pogoda

本文由黄兵的个人博客原创。

转载请注明出处:黄兵的个人博客 - 天气预报小程序

分享到:

发表评论

评论列表