您的位置:首页 > 其它

winform抓取页面,根据配置文件配置读取地址

2010-03-17 14:41 651 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Collections;

namespace Capture
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

Timer timer1 = new Timer();

private void Form1_Load(object sender, EventArgs e)
{

}

private void btnStop_Click(object sender, EventArgs e)
{
timer1.Stop();
//Application.Exit();
}

static ReadIniFile file = new ReadIniFile(@"E:/Capture/Capture/Capture/config.ini");
static string inifilename = file.fileName;
static bool isexist = file.ExistINIFile();
private void btnStart_Click(object sender, EventArgs e)
{

string timeInter = "";
if (isexist)
{
timeInter = file.GetString("iniInter", "txtInter");
timer1.Interval = int.Parse(timeInter) * 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
else
{
MessageBox.Show("配置文件不存在!");
return;
}
}

private void timer1_Tick(object sender, EventArgs e)
{
//string DirName = "";
string DirPath = txtDir.Text.ToString();
//string CapUrl = txtUrl.Text.Trim();

Dictionary<string, string> arrayList = new Dictionary<string, string>();
arrayList.Add("BasketballBigSmallScore", "BasketballBigSmallScore");
arrayList.Add("BasketballHandicap", "BasketballHandicap");
arrayList.Add("BasketballWinScoreDiffer", "BasketballWinScoreDiffer");
arrayList.Add("BasketballWL", "BasketballWL");
arrayList.Add("FootballhalfWholeMatch", "FootballhalfWholeMatch");
arrayList.Add("FootballScores", "FootballScores");
arrayList.Add("FootballSoccerWDL", "FootballSoccerWDL");
arrayList.Add("FootballTotalballNum", "FootballTotalballNum");

//string CapUrl = file.GetString(arrayList[i]);

foreach (KeyValuePair<string, string> key in arrayList)
{
string CapUrl = file.GetString(key.Key.ToString(), key.Value.ToString());
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(CapUrl));
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader str = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("gb2312"));
//string fileName = txtDir.Text.ToString().Substring(txtDir.Text.ToString().LastIndexOf("//") + 1);
//if (fileName == "" || !fileName.Contains(".html") || !fileName.Contains(".htm"))
//{

string URLName = CapUrl.Substring(CapUrl.LastIndexOf("/") + 1);
string myName = URLName.Substring(0, URLName.LastIndexOf(".")) + ".html";
//DirPath += "//" + URLName;

//}
// DirName = txtDir.Text.ToString().Substring(0, txtDir.Text.ToString().Length - fileName.Length);

if (!Directory.Exists(DirPath))
{
Directory.CreateDirectory(DirPath);
}
using (FileStream fs = new FileStream(DirPath + "//" + myName, FileMode.Create))
{
// 创建一个StreamWriter对象,使用UTF-8编码格式
using (StreamWriter writer = new StreamWriter(fs, Encoding.GetEncoding("gb2312")))
{
writer.Write(str.ReadToEnd());
writer.Close();
}
}

str.Close();
webResponse.Close();

}

}

private void btnBrowse_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtDir.Text = folderBrowserDialog1.SelectedPath;
}
}

}

class ReadIniFile
{
public string fileName;
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string lpAppName,// 指向包含 Section 名称的字符串地址
string lpKeyName,// 指向包含 Key 名称的字符串地址
string lpDefault,// 如果 Key 值没有找到,则返回缺省的字符串的地址
StringBuilder lpReturnedString,// 返回字符串的缓冲区地址
int nSize,// 缓冲区的长度
string lpFileName
);

[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFileName
);

public ReadIniFile(string filename)
{
this.fileName = filename;

}
public string GetString(string section, string key)
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(section, key, "", temp, 1024, this.fileName);
return temp.ToString();

}

public bool ExistINIFile()
{
return File.Exists(this.fileName);
}
public string FileName()
{
return this.fileName;
}
public void WriteInt(string section, string key, int iVal)
{
WritePrivateProfileString(section, key, iVal.ToString(), fileName);
}
public void WriteString(string section, string key, string strVal)
{
WritePrivateProfileString(section, key, strVal, fileName);
}

public void DelKey(string section, string key)
{
WritePrivateProfileString(section, key, null, fileName);
}
public void DelSection(string section)
{
WritePrivateProfileString(section, null, null, fileName);
}

}
}

配置文件:config.ini

[iniInter]
txtInter=30

[BasketballBigSmallScore]
BasketballBigSmallScore="http://info.sporttery.cn/lotterygame/hilo_list.php"

[BasketballHandicap]
BasketballHandicap="http://info.sporttery.cn/lotterygame/hdc_list.php"

[BasketballWinScoreDiffer]
BasketballWinScoreDiffer="http://info.sporttery.cn/lotterygame/wnm_list.php"

[BasketballWL]
BasketballWL="http://info.sporttery.cn/lotterygame/mnl_list.php"

[FootballhalfWholeMatch]
FootballhalfWholeMatch="http://info.sporttery.cn/lotterygame/hafu_list.php"

[FootballScores]
FootballScores="http://info.sporttery.cn/lotterygame/crs_list.php"

[FootballSoccerWDL]
FootballSoccerWDL="http://info.sporttery.cn/lotterygame/hhad_list.php"

[FootballTotalballNum]
FootballTotalballNum="http://info.sporttery.cn/lotterygame/ttg_list.php"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: