自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

壓縮備份C#工程

開(kāi)發(fā) 后端
這里介紹壓縮備份C#工程,雖然有源代碼管理,但本著所有重要的計(jì)算機(jī)文件都要備份的原則,但我們?nèi)匀恍枰獣r(shí)常將程序整體備份。

壓縮備份C#工程

雖然有源代碼管理,但本著所有重要的計(jì)算機(jī)文件都要備份的原則,但我們?nèi)匀恍枰獣r(shí)常將程序整體備份,一般的程序備份就是將程序目錄整個(gè)的復(fù)制打包,里面可能存在很多垃圾文件,而且是手工操作,比較麻煩,于是我們程序員就想到編個(gè)小程序來(lái)備份程序了。為了使用方便這個(gè)程序還能掛靠到集成開(kāi)發(fā)環(huán)境,方便隨時(shí)調(diào)用。

一般的我們都是用VS.NET作為開(kāi)發(fā)環(huán)境,因此這個(gè)小程序就要成為VS.NET的擴(kuò)展程序。但編寫(xiě)VS.NET的擴(kuò)展程序不是很方便,于是我們就想到更方便的擴(kuò)展VS.NET的方法,那就是VBA.NET。

VBA.NET是擴(kuò)展VS.NET的方法,是Office的VBA在VS.NET中的延續(xù)。使用方便,和VS.NET緊密結(jié)合,而且是解釋運(yùn)行,調(diào)試方便,而且其中的函數(shù)能綁定到VS.NET的工具條和菜單上面,調(diào)用方便。

現(xiàn)在說(shuō)說(shuō)壓縮備份C#工程的流程。以下以VS.NET2003為例子進(jìn)行說(shuō)明,本文中的代碼也只能處理VS.NET2003的C#工程。用記事本打開(kāi)一個(gè)VS.NET2003的C#工程文件(擴(kuò)展名為 .csproj ),可以看到這是一個(gè)XML文件,但這個(gè)XML文件沒(méi)有XML聲明頭"<?xml version='1.0' encoding='編碼格式' ?>",但它的編碼格式是GB2312,而.NET框架加載XML文件時(shí)若沒(méi)有找到XML聲明頭則使用的默認(rèn)編碼格式是UTF8,因此不能直接使用 System.XML.XmlDocument.Load 加載該文件。在此程序?qū)⑹褂肎B2312編碼格式(該編碼格式在.NET中的代碼為936)把C#工程文件當(dāng)作一個(gè)文本文件讀取其中所有的文本內(nèi)容,然后使用System.Xml.XmlDocument.LoadXml 加載XML文檔。

C#工程XML文檔中,從根節(jié)點(diǎn)出發(fā),路徑 VisualStudioProject/CSHARP/Build/Referencds/Reference 是指明工程使用的引用,也就是使用的DLL的文件名。而路徑 VisualStudioProject/CSHARP/Files/Include/File 則列出了工程中包含的所有的文件。程序?qū)⒗眠@兩個(gè)信息來(lái)獲得要拷貝的文件。此時(shí)程序拷貝所得的是干凈的C#項(xiàng)目,包含在C#項(xiàng)目目錄下的其他文件就不拷貝了。

程序把文件拷貝到一個(gè)臨時(shí)目錄后就調(diào)用WinRar的命令行程序來(lái)壓縮工程文件。如此完成壓縮備份C#工程。

點(diǎn)擊VS.NET的菜單項(xiàng)目"工具->宏->宏IDE",打開(kāi)了VS.NET的VBA.NET的集成開(kāi)發(fā)環(huán)境,編寫(xiě)代碼,然后切換到VS.NET的IDE,在工具條上右擊彈出快捷菜單,選擇最下面的"自定義"菜單項(xiàng)目,切換到"命令"頁(yè)面,在左邊的列表中選擇"宏",在右邊的列表中選中剛剛寫(xiě)好的VBA.NET的函數(shù),然后將其拖拽到VS.NET的工具條上,即可完成工具條按鈕和VBA.NET函數(shù)的綁定,此后你只有點(diǎn)擊這個(gè)按鈕就能壓縮備份你當(dāng)前編輯的C#工程了,實(shí)在是太方便了.以下就是操作過(guò)程的演示錄像.

完整的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. //設(shè)置要保存生成的文件的目錄  
  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 壓縮軟件的可執(zhí)行文件名,若 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, "系統(tǒng)提示")  
  32. End If  
  33. End Sub  
  34.  
  35. //將當(dāng)前編輯的VS.NET2003的C#工程整體復(fù)制到用戶指定的目錄下,不支持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("請(qǐng)輸入輸出目錄名稱", "輸入")  
  44. If strPath Is Nothing Then  
  45. Return  
  46. End If  
  47. If strPath.Length = 0 Then  
  48. Return  
  49. End If  
  50. //復(fù)制文件  
  51. Dim iCount As Integer = CopyCSProjectFiles(strPath)  
  52.    
  53. MsgBox("共拷貝 " & iCount & " 個(gè)文件")  
  54.    
  55. End Sub  
  56.    
  57. //復(fù)制當(dāng)前VS.NET2003的C#工程的所有包含的文件到指定的目錄下,不支持VS.NET2005  
  58. //不復(fù)制項(xiàng)目中使用絕對(duì)路徑引用的文件  
  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. //加載項(xiàng)目文件  
  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. //復(fù)制項(xiàng)目定義文件本身  
  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. //復(fù)制引用的文件  
  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. //復(fù)制項(xiàng)目文件  
  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 & " 正在復(fù)制文件 " & strFileName  
  102. End If  
  103. End If  
  104. Next  
  105. Return FileCount  
  106. End Function  
  107.  
  108.  
  109. //檢查當(dāng)前編輯的工程是不是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("當(dāng)前工程不是 C# 工程", MsgBoxStyle.Information, "系統(tǒng)提示")  
  114. Return False  
  115. End If  
  116. Return True  
  117. End Function  
  118.  
  119. //創(chuàng)建指定的目錄  
  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. //將指定目錄下的指定相對(duì)路徑的文件復(fù)制到另一個(gè)目錄,保持相對(duì)路徑不變  
  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. //復(fù)制指定的文件到指定的目錄下  
  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#對(duì)象初始化學(xué)習(xí)總結(jié)
  3. 使用C#正則表達(dá)式匹配相關(guān)字符串
  4. C#改寫(xiě)方法學(xué)習(xí)筆記
  5. 概述C#加框和消框
責(zé)任編輯:佚名 來(lái)源: csdn
相關(guān)推薦

2009-08-28 13:03:55

C#壓縮Access數(shù)

2009-08-28 16:29:02

C#類庫(kù)工程

2010-05-31 10:56:48

MySQL數(shù)據(jù)庫(kù)

2012-12-26 09:31:44

C#Winform

2009-08-25 17:15:50

C#隱藏C#重寫(xiě)C#重載

2017-02-27 19:57:02

Linux備份壓縮命令

2009-09-02 17:10:45

C#語(yǔ)言入門

2009-08-25 17:21:31

C#索引

2009-08-10 18:00:30

C#數(shù)據(jù)庫(kù)備份及還原

2009-08-25 17:59:49

C#入門

2009-08-13 17:04:09

C#語(yǔ)言C#程序

2009-08-27 16:11:03

C# delegateC# event

2009-08-18 10:30:30

C#枚舉

2009-08-24 11:02:52

C#接口映射

2009-08-26 10:34:15

C#類型C#變量

2009-08-24 09:55:26

C#接口轉(zhuǎn)換

2009-08-19 16:50:32

Visual C#C#語(yǔ)言特性

2016-10-13 13:33:41

反射特性c#

2009-08-28 10:14:45

C#內(nèi)存泄露

2009-12-15 14:08:14

Linux系統(tǒng)備份ta
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)