压缩备份C#工程

开发 后端
这里介绍压缩备份C#工程,虽然有源代码管理,但本着所有重要的计算机文件都要备份的原则,但我们仍然需要时常将程序整体备份。

压缩备份C#工程

虽然有源代码管理,但本着所有重要的计算机文件都要备份的原则,但我们仍然需要时常将程序整体备份,一般的程序备份就是将程序目录整个的复制打包,里面可能存在很多垃圾文件,而且是手工操作,比较麻烦,于是我们程序员就想到编个小程序来备份程序了。为了使用方便这个程序还能挂靠到集成开发环境,方便随时调用。

一般的我们都是用VS.NET作为开发环境,因此这个小程序就要成为VS.NET的扩展程序。但编写VS.NET的扩展程序不是很方便,于是我们就想到更方便的扩展VS.NET的方法,那就是VBA.NET。

VBA.NET是扩展VS.NET的方法,是Office的VBA在VS.NET中的延续。使用方便,和VS.NET紧密结合,而且是解释运行,调试方便,而且其中的函数能绑定到VS.NET的工具条和菜单上面,调用方便。

现在说说压缩备份C#工程的流程。以下以VS.NET2003为例子进行说明,本文中的代码也只能处理VS.NET2003的C#工程。用记事本打开一个VS.NET2003的C#工程文件(扩展名为 .csproj ),可以看到这是一个XML文件,但这个XML文件没有XML声明头"<?xml version='1.0' encoding='编码格式' ?>",但它的编码格式是GB2312,而.NET框架加载XML文件时若没有找到XML声明头则使用的默认编码格式是UTF8,因此不能直接使用 System.XML.XmlDocument.Load 加载该文件。在此程序将使用GB2312编码格式(该编码格式在.NET中的代码为936)把C#工程文件当作一个文本文件读取其中所有的文本内容,然后使用System.Xml.XmlDocument.LoadXml 加载XML文档。

C#工程XML文档中,从根节点出发,路径 VisualStudioProject/CSHARP/Build/Referencds/Reference 是指明工程使用的引用,也就是使用的DLL的文件名。而路径 VisualStudioProject/CSHARP/Files/Include/File 则列出了工程中包含的所有的文件。程序将利用这两个信息来获得要拷贝的文件。此时程序拷贝所得的是干净的C#项目,包含在C#项目目录下的其他文件就不拷贝了。

程序把文件拷贝到一个临时目录后就调用WinRar的命令行程序来压缩工程文件。如此完成压缩备份C#工程。

点击VS.NET的菜单项目"工具->宏->宏IDE",打开了VS.NET的VBA.NET的集成开发环境,编写代码,然后切换到VS.NET的IDE,在工具条上右击弹出快捷菜单,选择最下面的"自定义"菜单项目,切换到"命令"页面,在左边的列表中选择"宏",在右边的列表中选中刚刚写好的VBA.NET的函数,然后将其拖拽到VS.NET的工具条上,即可完成工具条按钮和VBA.NET函数的绑定,此后你只有点击这个按钮就能压缩备份你当前编辑的C#工程了,实在是太方便了.以下就是操作过程的演示录像.

完整的VBA.NET源代码为

  1. Public Sub CreateCSProjectRAR()  
  2. If CheckCSProject() = False Then  
  3. Return  
  4. End If  
  5. Dim strPath As String  
  6. Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project  
  7.  
  8. strPath = System.IO.Path.GetFileNameWithoutExtension(myPrj.FullName)  
  9.  
  10. Dim strFileName As String  
  11. //设置要保存生成的文件的目录  
  12. strPath = System.IO.Path.Combine
    ("D:\SourceBack", strPath & "[" & System.DateTime.Now.ToString("yyyy-MM-dd") & "]")  
  13. strFileName = strPath & ".rar"  
  14.  
  15. If System.IO.File.Exists(strFileName) Then  
  16. System.IO.File.Delete(strFileName)  
  17. End If  
  18. Dim iCount As Integer = CopyCSProjectFiles(strPath)  
  19. If System.IO.File.Exists(strFileName) Then  
  20. System.IO.File.Delete(strFileName)  
  21. End If  
  22. If iCount > 0 Then  
  23. DTE.StatusBar.Text = "正在生成压缩文件" 
  24. Dim start As New System.Diagnostics.ProcessStartInfo  
  25. //此处指定 WinRar 压缩软件的可执行文件名,若 WinRar安装在其他的目录则修改此文件名  
  26. start.FileName = "C:\Program Files\WinRAR\WinRAR.exe" 
  27. start.Arguments = "a -r -df -ep1 " & strFileName & " " & strPath  
  28. Dim p As SystemSystem.Diagnostics.Process = System.Diagnostics.Process.Start(start)  
  29. p.WaitForExit()  
  30. DTE.StatusBar.Text = "已生成压缩文件 " & strFileName  
  31. MsgBox("已生成压缩文件 " & strFileName, MsgBoxStyle.Information, "系统提示")  
  32. End If  
  33. End Sub  
  34.  
  35. //将当前编辑的VS.NET2003的C#工程整体复制到用户指定的目录下,不支持VS.NET2005  
  36. Public Sub CopyCSProject()  
  37.  
  38. //检查是否是C#工程  
  39. If CheckCSProject() = False Then  
  40. Return  
  41. End If  
  42. //让用户输入目录  
  43. Dim strPath As String = InputBox("请输入输出目录名称", "输入")  
  44. If strPath Is Nothing Then  
  45. Return  
  46. End If  
  47. If strPath.Length = 0 Then  
  48. Return  
  49. End If  
  50. //复制文件  
  51. Dim iCount As Integer = CopyCSProjectFiles(strPath)  
  52.    
  53. MsgBox("共拷贝 " & iCount & " 个文件")  
  54.    
  55. End Sub  
  56.    
  57. //复制当前VS.NET2003的C#工程的所有包含的文件到指定的目录下,不支持VS.NET2005  
  58. //不复制项目中使用绝对路径引用的文件  
  59. Public Function CopyCSProjectFiles(ByVal strPath As String) As Integer  
  60.    
  61. If CheckCSProject() = False Then  
  62. Return -1  
  63. End If  
  64.    
  65. If System.IO.Directory.Exists(strPath) = False Then  
  66. System.IO.Directory.CreateDirectory(strPath)  
  67. End If  
  68. Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project  
  69.    
  70. //加载项目文件  
  71. Dim myFile As New System.IO.StreamReader
    (myPrj.FullName, System.Text.Encoding.GetEncoding(936))  
  72. Dim myDoc As New System.Xml.XmlDocument  
  73. myDoc.LoadXml(myFile.ReadToEnd())  
  74. myFile.Close()  
  75.    
  76. Dim ThisPath As String = System.IO.Path.GetDirectoryName(myPrj.FullName)  
  77.    
  78. //复制项目定义文件本身  
  79. CopyFile(myPrj.FullName, strPath)  
  80.    
  81. Dim FileCount As Integer  
  82. Dim myElement As System.Xml.XmlElement  
  83. Dim strFileName As String  
  84. Dim strNewFileName As String  
  85. //复制引用的文件  
  86. For Each myElement In myDoc.SelectNodes
    ("VisualStudioProject/CSHARP/Build/Referencds/Reference")  
  87. strFileName = myElement.GetAttribute("HintPath")  
  88. If System.IO.Path.IsPathRooted(strFileName) = False Then  
  89. CopyFile(ThisPath, strPath, strFileName)  
  90. FileCountFileCount = FileCount + 1  
  91. End If  
  92. Next  
  93.    
  94. //复制项目文件  
  95. For Each myElement In myDoc.SelectNodes
    ("VisualStudioProject/CSHARP/Files/Include/File")  
  96. strFileName = myElement.GetAttribute("RelPath")  
  97. If Not strFileName Is Nothing Then  
  98. If System.IO.Path.IsPathRooted(strFileName) = False Then  
  99. CopyFile(ThisPath, strPath, strFileName)  
  100. FileCountFileCount = FileCount + 1  
  101. DTE.StatusBar.Text = FileCount & " 正在复制文件 " & strFileName  
  102. End If  
  103. End If  
  104. Next  
  105. Return FileCount  
  106. End Function  
  107.  
  108.  
  109. //检查当前编辑的工程是不是C#工程  
  110. Public Function CheckCSProject() As Boolean  
  111. Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project  
  112. If UCase(System.IO.Path.GetExtension(myPrj.FullName)) <> ".CSPROJ" Then  
  113. MsgBox("当前工程不是 C# 工程", MsgBoxStyle.Information, "系统提示")  
  114. Return False  
  115. End If  
  116. Return True  
  117. End Function  
  118.  
  119. //创建指定的目录  
  120. Public Sub CreateDirectory(ByVal strDir As String)  
  121. If System.IO.Directory.Exists(strDir) = False Then  
  122. System.IO.Directory.CreateDirectory(strDir)  
  123. End If  
  124. End Sub  
  125.  
  126. //将指定目录下的指定相对路径的文件复制到另一个目录,保持相对路径不变  
  127. Public Sub CopyFile(ByVal strPath1 As String, 
    ByVal strPath2 As String, ByVal strFilePath As String)  
  128. Dim strName1 As String = System.IO.Path.Combine(strPath1, strFilePath)  
  129. Dim strName2 As String = System.IO.Path.Combine(strPath2, strFilePath)  
  130.  
  131. Dim dir1 As String = System.IO.Path.GetDirectoryName(strName1)  
  132. If System.IO.Directory.Exists(dir1) = False Then  
  133. System.IO.Directory.CreateDirectory(dir1)  
  134. End If  
  135.  
  136. Dim dir2 As String = System.IO.Path.GetDirectoryName(strName2)  
  137. If System.IO.Directory.Exists(dir2) = False Then  
  138. System.IO.Directory.CreateDirectory(dir2)  
  139. End If  
  140.  
  141. System.IO.File.Copy(strName1, strName2, True)  
  142.  
  143. End Sub  
  144.  
  145. //复制指定的文件到指定的目录下  
  146. Public Sub CopyFile(ByVal strFileName As String, ByVal strNewPath As String)  
  147. System.IO.File.Copy(strFileName, System.IO.Path.Combine
    (strNewPath, System.IO.Path.GetFileName(strFileName)), True)  
  148. End Sub  

【编辑推荐】

  1. 浅析C# ArrayList
  2. C#对象初始化学习总结
  3. 使用C#正则表达式匹配相关字符串
  4. C#改写方法学习笔记
  5. 概述C#加框和消框
责任编辑:佚名 来源: csdn
相关推荐

2009-08-28 13:03:55

C#压缩Access数

2009-08-28 16:29:02

C#类库工程

2012-12-26 09:31:44

C#Winform

2010-05-31 10:56:48

MySQL数据库

2017-02-27 19:57:02

Linux备份压缩命令

2009-08-25 17:15:50

C#隐藏C#重写C#重载

2009-08-25 17:21:31

C#索引

2009-09-02 17:10:45

C#语言入门

2009-08-10 18:00:30

C#数据库备份及还原

2009-08-27 16:11:03

C# delegateC# event

2009-08-13 17:04:09

C#语言C#程序

2009-08-25 17:59:49

C#入门

2009-12-15 14:08:14

Linux系统备份ta

2009-08-19 16:50:32

Visual C#C#语言特性

2009-08-24 09:55:26

C#接口转换

2009-08-18 10:30:30

C#枚举

2009-08-26 10:34:15

C#类型C#变量

2009-08-24 11:02:52

C#接口映射

2016-10-13 13:33:41

反射特性c#

2009-08-28 10:14:45

C#内存泄露
点赞
收藏

51CTO技术栈公众号