日期:2014-05-17  浏览次数:20986 次

C# 自动更新下载文件列表
namespace UpLoad
{
  public partial class DownList : Form
  {
  DataTable dt = null;
  public DownList()
  {
  InitializeComponent();
  }

  private void DownList_Load(object sender, EventArgs e)
  {
  dt = getSource();
  dataGridView1.DataSource = dt;
  }
   
  public DataTable getSource()
  {


   
  string url = Form1.GetSourceList(System.AppDomain.CurrentDomain.BaseDirectory + "UpXml.xml", "url")[0];
  List<string> list = Form1.GetSourceList(url, "UpdateFile");
  DataTable tblDatas = new DataTable("Datas");
  DataColumn dc = null;
  string url2 = Form1.GetSourceList(url, "ut")[0];
  //赋值给dc,是便于对每一个datacolumn的操作
  dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
  dc.AutoIncrement = true;//自动增加
  dc.AutoIncrementSeed = 1;//起始为1
  dc.AutoIncrementStep = 1;//步长为1
  dc.AllowDBNull = false;//
  dc = tblDatas.Columns.Add("name", Type.GetType("System.String"));
  dc = tblDatas.Columns.Add("lenght", Type.GetType("System.String"));
  dc = tblDatas.Columns.Add("url", Type.GetType("System.String"));
  DataRow newRow;
  for (int i = 0; i < list.Count; i++)
  {
  newRow = tblDatas.NewRow();
  newRow["name"] = list[i];
  newRow["lenght"] = list[i].ToString().Length;
  newRow["url"] =url2+"//"+ list[i].ToString();
  tblDatas.Rows.Add(newRow);
  }
  return tblDatas;
  }
  //设置进度条
  public void setProgressBar()
  {
  progressBar1.Value = 0;
  progressBar1.Minimum = 0;
  progressBar1.Maximum = dt.Rows.Count;
  progressBar1.Step = 1;
  for (int i = 0; i < dt.Rows.Count; i++)
  {
  progressBar1.Value += 1;
  WebClient client = new WebClient();
  client.DownloadFile(dt.Rows[0]["url"].ToString(), dt.Rows[i]["name"].ToString());
  Thread.Sleep(1000);
  }
  }
  private void button1_Click(object sender, EventArgs e)
  {
  setProgressBar();
  }
downlist 界面Demo 
public static List<string> GetSourceList(string xmlfilePath, string nodeName)
  {
  XmlTextReader textReader = null;
  List<string> listStr = new List<string>();
  try
  {
  textReader = new XmlTextReader(xmlfilePath);

  while (textReader.Read())
  {
  if (textReader.NodeType == XmlNodeType.Element && textReader.IsStartElement())
  {
  if (textReader.Name == nodeName)
  {