不用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"
以下代码生成命令行:
- public static string GenerateRarCommandLine(
- bool setPassword,
- string password,
- string param,
- string strategyPath,
- string workPath,
- string objectFilePath,
- string sourcePath
- )
- {
- StringBuilder s = new StringBuilder();
- s.Append("a -m5 -ep1 ");
- if (setPassword)
- {
- s.Append("-p");
- s.Append(password);
- s.Append(" ");
- }
- s.Append(param);
- s.Append(" ");
- s.Append("-n@/"");
- s.Append(strategyPath);
- s.Append("/" ");
- s.Append("-w/"");
- s.Append(workPath);
- s.Append("/" /"");
- s.Append(objectFilePath);
- s.Append("/" /"");
- s.Append(sourcePath); //目录名最后不能有斜杠
- s.Append("/"");
- return s.ToString();
- }
调用RAR完成备份:
- public string Execute(string filename, string dosCommand, int milliseconds)
- {
- string output = ""; //输出字符串
- if (dosCommand != null && dosCommand != "")
- {
- Process process = new Process(); //创建进程对象
- ProcessStartInfo startInfo = new ProcessStartInfo();
- startInfo.FileName = filename; //设定需要执行的命令
- startInfo.Arguments = " " + dosCommand; //设定参数
- startInfo.UseShellExecute = false; //不使用系统外壳程序启动
- startInfo.RedirectStandardInput = false; //不重定向输入
- startInfo.RedirectStandardOutput = true; //重定向输出
- startInfo.CreateNoWindow = true; //不创建窗口
- process.StartInfo = startInfo;
- try
- {
- if (process.Start()) //开始进程
- {
- if (milliseconds == 0)
- process.WaitForExit(); //这里无限等待进程结束
- else
- process.WaitForExit(milliseconds); //这里等待进程结束,等待时间为指定的毫秒
- output = process.StandardOutput.ReadToEnd();//读取进程的输出
- }
- }
- catch
- {
- }
- finally
- {
- if (process != null)
- process.Close();
- }
- }
- return output;
- }
截图:
备份结果:
备份策略用于定义要备份的文件(用于去除IDE的各种临时文件),可自由定制,保存与pattern目录下,格式如下:
文本文件,每行一个要备份的文件,可使用通配符,例如
VC2005工程.pattern
*.c
*.cpp
*.h
*.hpp
*.rc
*.sln
*.vcproj
*.def
*makefile
*sources
*dir