CSDN博客

img keenx

整盘备份的小项目(c#)

发表于2004/7/14 15:41:00  1104人阅读

app.cs

using System;
using System.IO;

namespace com.todayisp.bakup
{
 /// <summary>
 /// app 的摘要说明。
 /// </summary>
 public class app
 {
  //列举某个盘下面的所有目录,不包括文件。
  //目录路径pathstr,如e:/
  //返回以目录组成的数组。
  public string[] Directorys(string pathstr)
  {
   if(pathstr==null) return null;
   if(!Directory.Exists(pathstr))
   {
    Console.WriteLine("error:filepath not exist--"+pathstr);
    return null;
   }
   Console.WriteLine("start check path");
   string[] filestrs=Directory.GetDirectories(pathstr);
   for(int i=0;i<filestrs.Length;i++)
   {
    Console.WriteLine(filestrs[i]);
   }
   return filestrs;
  }


  //对目录进行压缩,采用rar.exe+命令行方式来处理。
  //rarName:要压缩成的rar文件路径及名称,如e:/abc.rar
  //dirName:要压缩的目录,如e:/abc
  public bool bak(string rarName,string dirName)
  {
   bool blnRst = false;
   string strArg = "a -r  {0} {1}";
   System.Diagnostics.Process p = new System.Diagnostics.Process();
   p.StartInfo.FileName = @"e:/rar.exe";//exe,bat and so on
   p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
   p.StartInfo.Arguments = String.Format(strArg, rarName, dirName);
   try
   {
    p.Start();
    p.WaitForExit();
    p.Close();
    blnRst = true;
   }
   catch
   {
   }
   return blnRst;
  }

  //压缩日志,每压缩一个文件夹就写入日志文件。
  //filestr日志路径,content日志内容
  public bool writeLog(string filestr,string content)
  {
   try
   {
    StreamWriter sw = new StreamWriter(filestr,true,System.Text.UTF8Encoding.UTF8);
    sw.WriteLine(content);
    sw.Close();
    
    return true;
   }
   catch
   {
    return false;
   }
  }

  //读取日志,分析日志,得出上次备份时间,从而可以选择是否操作
  //logPath日志路径,dirName要备份的目录名。
  public bool readLog(string logPath,string dirName,int intdate)
  {
   try
   {
    string str;
    StreamReader din = File.OpenText(logPath);
    while ((str = din.ReadLine()) != null)
    {
     string[] strs=str.Split(' ');
     if(strs[1]==dirName.ToLower())
     {
      Console.WriteLine(strs[0]+":"+strs[1]);
      Console.WriteLine(DateTime.Today.ToShortDateString());
      if(Convert.ToDateTime(DateTime.Today.ToShortDateString())<Convert.ToDateTime(strs[0]).AddDays(intdate))
      {
       din.Close();
       return false;
      }
     }
    }
    din.Close();
    return true;
   }
   catch{
    return false;
   }
  }
 }
}

 

//class1.cs程序入口

using System;
using System.IO;
using System.Configuration;

namespace com.todayisp.bakup
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class Class1
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   //
   // TODO: 在此处添加代码以启动应用程序
   //
   app App = new app();

   string pathstr = ConfigurationSettings.AppSettings["dir"];
   string maxcontent = ConfigurationSettings.AppSettings["maxcontent"];
   string logpath = ConfigurationSettings.AppSettings["logpath"];
   int bakupday = Convert.ToInt32(ConfigurationSettings.AppSettings["bakupday"]);
   string[] dir = App.Directorys(pathstr);
   if (dir==null) return;

   long content = 0;
   
   for(int i=0;i<dir.Length;i++)
   {
    try
    {
     if(content>=Convert.ToInt32(maxcontent)) return;
     string dirName=dir[i].Trim();
     string rarPath=dirName+".rar";
     int pos = rarPath.LastIndexOf(@"/");
     string rarName = rarPath.Substring(pos+1,rarPath.Length-pos-1);

     if(App.readLog(logpath,dirName,bakupday))   
     {
      bool bakup = App.bak(rarPath,dirName);
      bool bakup2 = App.writeLog(logpath,DateTime.Today.ToShortDateString()+" "+rarName+" "+dirName.ToLower());
      Console.WriteLine("Bakup "+dirName+" to "+rarName+" "+bakup+".Write to log "+bakup2);
      FileInfo fi = new FileInfo(rarPath);
      content = content + fi.Length;
      Console.WriteLine(content+"bytes");
     }
     else
     {
      Console.WriteLine("No need to bukup "+dirName);
     }
    }
    catch(Exception ex)
    {
     Console.WriteLine(ex.Message);
    }
   }

  }
  
 }
}

 

//bakup.exe.config配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <!-- application specific settings -->

    <system.web>
        <customErrors mode="Off"/>
    </system.web>

    <appSettings>
 <add key="dir" value="e:/rui"  />
 <add key="maxcontent" value="2000" />
 <add key="logpath" value="e:/abc.txt" />
 <add key="bakupday" value="1" />
    </appSettings>
</configuration>

0 0

相关博文

我的热门文章

img
取 消
img