使用VBA.NET壓縮備份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源代碼為
2 ' 本函數調用 WinRar 軟件進行壓縮,因此計算機系統必須安裝 WinRar
3 ' 程序編制 袁永福(http://www.xdesigner.cn) 2007-8-7
4 Public Sub CreateCSProjectRAR()
5 If CheckCSProject() = False Then
6 Return
7 End If
8 Dim strPath As String
9 Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project
10
11 strPath = System.IO.Path.GetFileNameWithoutExtension(myPrj.FullName)
12
13 Dim strFileName As String
14 ' 設置要保存生成的文件的目錄
15 strPath = System.IO.Path.Combine("D:\SourceBack", strPath & "[" & System.DateTime.Now.ToString("yyyy-MM-dd") & "]")
16 strFileName = strPath & ".rar"
17
18 If System.IO.File.Exists(strFileName) Then
19 System.IO.File.Delete(strFileName)
20 End If
21 Dim iCount As Integer = CopyCSProjectFiles(strPath)
22 If System.IO.File.Exists(strFileName) Then
23 System.IO.File.Delete(strFileName)
24 End If
25 If iCount > 0 Then
26 DTE.StatusBar.Text = "正在生成壓縮文件
"27 Dim start As New System.Diagnostics.ProcessStartInfo
28 ' 此處指定 WinRar 壓縮軟件的可執行文件名,若 WinRar安裝在其他的目錄則修改此文件名
29 start.FileName = "C:\Program Files\WinRAR\WinRAR.exe"
30 start.Arguments = "a -r -df -ep1 " & strFileName & " " & strPath
31 Dim p As System.Diagnostics.Process = System.Diagnostics.Process.Start(start)
32 p.WaitForExit()
33 DTE.StatusBar.Text = "已生成壓縮文件 " & strFileName
34 MsgBox("已生成壓縮文件 " & strFileName, MsgBoxStyle.Information, "系統提示")
35 End If
36 End Sub
37
38 ' 將當前編輯的VS.NET2003的C#工程整體復制到用戶指定的目錄下,不支持VS.NET2005
39 Public Sub CopyCSProject()
40
41 ' 檢查是否是C#工程
42 If CheckCSProject() = False Then
43 Return
44 End If
45 ' 讓用戶輸入目錄
46 Dim strPath As String = InputBox("請輸入輸出目錄名稱", "輸入")
47 If strPath Is Nothing Then
48 Return
49 End If
50 If strPath.Length = 0 Then
51 Return
52 End If
53 ' 復制文件
54 Dim iCount As Integer = CopyCSProjectFiles(strPath)
55
56 MsgBox("共拷貝 " & iCount & " 個文件")
57
58 End Sub
59
60 ' 復制當前VS.NET2003的C#工程的所有包含的文件到指定的目錄下,不支持VS.NET2005
61 ' 不復制項目中使用絕對路徑引用的文件
62 Public Function CopyCSProjectFiles(ByVal strPath As String) As Integer
63
64 If CheckCSProject() = False Then
65 Return -1
66 End If
67
68 If System.IO.Directory.Exists(strPath) = False Then
69 System.IO.Directory.CreateDirectory(strPath)
70 End If
71 Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project
72
73 ' 加載項目文件
74 Dim myFile As New System.IO.StreamReader(myPrj.FullName, System.Text.Encoding.GetEncoding(936))
75 Dim myDoc As New System.Xml.XmlDocument
76 myDoc.LoadXml(myFile.ReadToEnd())
77 myFile.Close()
78
79 Dim ThisPath As String = System.IO.Path.GetDirectoryName(myPrj.FullName)
80
81 ' 復制項目定義文件本身
82 CopyFile(myPrj.FullName, strPath)
83
84 Dim FileCount As Integer
85 Dim myElement As System.Xml.XmlElement
86 Dim strFileName As String
87 Dim strNewFileName As String
88 ' 復制引用的文件
89 For Each myElement In myDoc.SelectNodes("VisualStudioProject/CSHARP/Build/Referencds/Reference")
90 strFileName = myElement.GetAttribute("HintPath")
91 If System.IO.Path.IsPathRooted(strFileName) = False Then
92 CopyFile(ThisPath, strPath, strFileName)
93 FileCount = FileCount + 1
94 End If
95 Next
96
97 ' 復制項目文件
98 For Each myElement In myDoc.SelectNodes("VisualStudioProject/CSHARP/Files/Include/File")
99 strFileName = myElement.GetAttribute("RelPath")
100 If Not strFileName Is Nothing Then
101 If System.IO.Path.IsPathRooted(strFileName) = False Then
102 CopyFile(ThisPath, strPath, strFileName)
103 FileCount = FileCount + 1
104 DTE.StatusBar.Text = FileCount & " 正在復制文件 " & strFileName
105 End If
106 End If
107 Next
108 Return FileCount
109 End Function
110
111
112 ' 檢查當前編輯的工程是不是C#工程
113 Public Function CheckCSProject() As Boolean
114 Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project
115 If UCase(System.IO.Path.GetExtension(myPrj.FullName)) <> ".CSPROJ" Then
116 MsgBox("當前工程不是 C# 工程", MsgBoxStyle.Information, "系統提示")
117 Return False
118 End If
119 Return True
120 End Function
121
122 ' 創建指定的目錄
123 Public Sub CreateDirectory(ByVal strDir As String)
124 If System.IO.Directory.Exists(strDir) = False Then
125 System.IO.Directory.CreateDirectory(strDir)
126 End If
127 End Sub
128
129 ' 將指定目錄下的指定相對路徑的文件復制到另一個目錄,保持相對路徑不變
130 Public Sub CopyFile(ByVal strPath1 As String, ByVal strPath2 As String, ByVal strFilePath As String)
131 Dim strName1 As String = System.IO.Path.Combine(strPath1, strFilePath)
132 Dim strName2 As String = System.IO.Path.Combine(strPath2, strFilePath)
133
134 Dim dir1 As String = System.IO.Path.GetDirectoryName(strName1)
135 If System.IO.Directory.Exists(dir1) = False Then
136 System.IO.Directory.CreateDirectory(dir1)
137 End If
138
139 Dim dir2 As String = System.IO.Path.GetDirectoryName(strName2)
140 If System.IO.Directory.Exists(dir2) = False Then
141 System.IO.Directory.CreateDirectory(dir2)
142 End If
143
144 System.IO.File.Copy(strName1, strName2, True)
145
146 End Sub
147
148 ' 復制指定的文件到指定的目錄下
149 Public Sub CopyFile(ByVal strFileName As String, ByVal strNewPath As String)
150 System.IO.File.Copy(strFileName, System.IO.Path.Combine(strNewPath, System.IO.Path.GetFileName(strFileName)), True)
151 End Sub
152
153
posted on 2007-08-07 15:35 袁永福 電子病歷,醫療信息化 閱讀(2229) 評論(3) 收藏 舉報
浙公網安備 33010602011771號