简单的源码备份工具

 

 

不用SVN之类工具的可以看看:

 

主要功能:

1、调用WinRar进行备份

2、可设置密码

3、可选择要备份的文件

4、对备份文件进行组织。

 

原理很简单,RAR命令行(对应下图):

"E:/WinRAR/Rar.exe" a -m5 -ep1 -n@"./VC2005工程.pattern" -w"F:/Projects/TestProjects/" "F:/Projects/TestProjects/Test.rar" "F:/Projects/TestProjects/Test"

 

以下代码生成命令行:

 

 

 

 

 

 

  1. public static string GenerateRarCommandLine(
  2.             bool setPassword,
  3.             string password,
  4.             string param,
  5.             string strategyPath,
  6.             string workPath,
  7.             string objectFilePath,
  8.             string sourcePath
  9.             )
  10.         {
  11.             StringBuilder s = new StringBuilder();
  12.             s.Append("a -m5 -ep1 ");
  13.             if (setPassword)
  14.             { 
  15.                 s.Append("-p"); 
  16.                 s.Append(password); 
  17.                 s.Append(" "); 
  18.             }
  19.             s.Append(param);
  20.             s.Append(" ");
  21.             s.Append("-n@/"");
  22.             s.Append(strategyPath);
  23.             s.Append("/" ");
  24.             s.Append("-w/"");
  25.             s.Append(workPath);
  26.             s.Append("/" /"");
  27.             s.Append(objectFilePath);
  28.             s.Append("/" /"");
  29.             s.Append(sourcePath); //目录名最后不能有斜杠
  30.             s.Append("/"");
  31.             return s.ToString();
  32.         }

调用RAR完成备份:

  1. public string Execute(string filename, string dosCommand, int milliseconds)
  2.         {
  3.             string output = "";     //输出字符串
  4.             if (dosCommand != null && dosCommand != "")
  5.             {
  6.                 Process process = new Process();     //创建进程对象
  7.                 ProcessStartInfo startInfo = new ProcessStartInfo();
  8.                 startInfo.FileName = filename;     //设定需要执行的命令
  9.                 startInfo.Arguments = " " + dosCommand;   //设定参数
  10.                 startInfo.UseShellExecute = false;     //不使用系统外壳程序启动
  11.                 startInfo.RedirectStandardInput = false;   //不重定向输入
  12.                 startInfo.RedirectStandardOutput = true;   //重定向输出
  13.                 startInfo.CreateNoWindow = true;     //不创建窗口
  14.                 process.StartInfo = startInfo;
  15.                 try
  16.                 {
  17.                     if (process.Start())       //开始进程
  18.                     {
  19.                         if (milliseconds == 0)
  20.                             process.WaitForExit();     //这里无限等待进程结束
  21.                         else
  22.                             process.WaitForExit(milliseconds);  //这里等待进程结束,等待时间为指定的毫秒
  23.                         output = process.StandardOutput.ReadToEnd();//读取进程的输出
  24.                     }
  25.                 }
  26.                 catch
  27.                 {
  28.                 }
  29.                 finally
  30.                 {
  31.                     if (process != null)
  32.                         process.Close();
  33.                 }
  34.             }
  35.             return output;
  36.         }

截图:

 

备份结果:

 

备份策略用于定义要备份的文件(用于去除IDE的各种临时文件),可自由定制,保存与pattern目录下,格式如下:

文本文件,每行一个要备份的文件,可使用通配符,例如

VC2005工程.pattern

*.c
*.cpp
*.h
*.hpp
*.rc
*.sln
*.vcproj
*.def
*makefile
*sources
*dir

 

 

下载地址:http://download.csdn.net/source/941239

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值