专注于网站技术与网络营销的博客
« DataGrid分页例子用DataReader还是DataSet »

用线程控制程序的执行

  今天要写一个Console程序,用于间断性将目录1下的所有符合要求的xml文件另存到目标目录2中,并对xml文件内容进行改写。
  读取和改写xml有XmlDocument类进行处理,但由于xml文件数量过多,而且操作上可以需要一定的时间,所以考虑用DataSet的ReadXml和WriteXml方法进行处理。工作上要求5分钟执行一次该程序,于是,这里应该用上Thread类。


用线程控制程序的执行 摘自:http://www.lemongtree.com/Detail,306.html 

public void Main()
  {
  //设定永远为真的一个循环
   while (true)
   {
    if (chkPath(_SourcePath) && (chkPath(_GoalPath)))
    {
     DirectoryInfo di = new DirectoryInfo(_SourcePath);
                 
     foreach (FileInfo fi in di.GetFiles())
     {
                       
      string xmlFileName = fi.Name;
      string FullName = fi.FullName;
      DataSet dst = new DataSet();
      dst.ReadXml(FullName);
      if (dst.Tables.Contains("channel") == false)
      {
       Console.WriteLine("no table " + FullName);
       continue;
      }
                      
      foreach (DataRow r in dst.Tables["channel"].Rows)
      {
                           
       string[] arrCatalog = r["Catalog"].ToString().Split(' ');
       r["catalog"] = arrCatalog[0];
                          
      }
      dst.WriteXml(_GoalPath + xmlFileName);
      dst.Dispose();
      dst = null;
                    
     }
    }
    else
    {
     System.Console.WriteLine("来源目录或目标目录配置错误!请检查配置文件");
     string s = Console.ReadLine();        
    }
     //设定5分钟执行一次
    Thread.Sleep(_SleepTime * 60 * 1000);
   }
  }

然后在
[STAThread]
  static void Main(string[] args)
中调用该方法即可
  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.8 Devo Build 80201

Copyright 2005 - 2008, 完美生活(专注于网站技术与网络营销的博客). Some Rights Reserved.