日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
壓縮備份C#工程

壓縮備份C#工程

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

一般的我們都是用VS.NET作為開發(fā)環(huán)境,因此這個小程序就要成為VS.NET的擴(kuò)展程序。但編寫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)在說說壓縮備份C#工程的流程。以下以VS.NET2003為例子進(jìn)行說明,本文中的代碼也只能處理VS.NET2003的C#工程。用記事本打開一個VS.NET2003的C#工程文件(擴(kuò)展名為 .csproj ),可以看到這是一個XML文件,但這個XML文件沒有XML聲明頭"",但它的編碼格式是GB2312,而.NET框架加載XML文件時若沒有找到XML聲明頭則使用的默認(rèn)編碼格式是UTF8,因此不能直接使用 System.XML.XmlDocument.Load 加載該文件。在此程序?qū)⑹褂肎B2312編碼格式(該編碼格式在.NET中的代碼為936)把C#工程文件當(dāng)作一個文本文件讀取其中所有的文本內(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ū)⒗眠@兩個信息來獲得要拷貝的文件。此時程序拷貝所得的是干凈的C#項(xiàng)目,包含在C#項(xiàng)目目錄下的其他文件就不拷貝了。

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

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

完整的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. strPath = System.IO.Path.GetFileNameWithoutExtension(myPrj.FullName)
  8. Dim strFileName As String
  9. //設(shè)置要保存生成的文件的目錄
  10. strPath = System.IO.Path.Combine
    ("D:\SourceBack", strPath & "[" & System.DateTime.Now.ToString("yyyy-MM-dd") & "]")
  11. strFileName = strPath & ".rar"
  12. If System.IO.File.Exists(strFileName) Then
  13. System.IO.File.Delete(strFileName)
  14. End If
  15. Dim iCount As Integer = CopyCSProjectFiles(strPath)
  16. If System.IO.File.Exists(strFileName) Then
  17. System.IO.File.Delete(strFileName)
  18. End If
  19. If iCount > 0 Then
  20. DTE.StatusBar.Text = "正在生成壓縮文件"
  21. Dim start As New System.Diagnostics.ProcessStartInfo
  22. //此處指定 WinRar 壓縮軟件的可執(zhí)行文件名,若 WinRar安裝在其他的目錄則修改此文件名
  23. start.FileName = "C:\Program Files\WinRAR\WinRAR.exe"
  24. start.Arguments = "a -r -df -ep1 " & strFileName & " " & strPath
  25. Dim p As SystemSystem.Diagnostics.Process = System.Diagnostics.Process.Start(start)
  26. p.WaitForExit()
  27. DTE.StatusBar.Text = "已生成壓縮文件 " & strFileName
  28. MsgBox("已生成壓縮文件 " & strFileName, MsgBoxStyle.Information, "系統(tǒng)提示")
  29. End If
  30. End Sub
  31. //將當(dāng)前編輯的VS.NET2003的C#工程整體復(fù)制到用戶指定的目錄下,不支持VS.NET2005
  32. Public Sub CopyCSProject()
  33. //檢查是否是C#工程
  34. If CheckCSProject() = False Then
  35. Return
  36. End If
  37. //讓用戶輸入目錄
  38. Dim strPath As String = InputBox("請輸入輸出目錄名稱", "輸入")
  39. If strPath Is Nothing Then
  40. Return
  41. End If
  42. If strPath.Length = 0 Then
  43. Return
  44. End If
  45. //復(fù)制文件
  46. Dim iCount As Integer = CopyCSProjectFiles(strPath)
  47.  
  48. MsgBox("共拷貝 " & iCount & " 個文件")
  49.  
  50. End Sub
  51.  
  52. //復(fù)制當(dāng)前VS.NET2003的C#工程的所有包含的文件到指定的目錄下,不支持VS.NET2005
  53. //不復(fù)制項(xiàng)目中使用絕對路徑引用的文件
  54. Public Function CopyCSProjectFiles(ByVal strPath As String) As Integer
  55.  
  56. If CheckCSProject() = False Then
  57. Return -1
  58. End If
  59.  
  60. If System.IO.Directory.Exists(strPath) = False Then
  61. System.IO.Directory.CreateDirectory(strPath)
  62. End If
  63. Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project
  64.  
  65. //加載項(xiàng)目文件
  66. Dim myFile As New System.IO.StreamReader
    (myPrj.FullName, System.Text.Encoding.GetEncoding(936))
  67. Dim myDoc As New System.Xml.XmlDocument
  68. myDoc.LoadXml(myFile.ReadToEnd())
  69. myFile.Close()
  70.  
  71. Dim ThisPath As String = System.IO.Path.GetDirectoryName(myPrj.FullName)
  72.  
  73. //復(fù)制項(xiàng)目定義文件本身
  74. CopyFile(myPrj.FullName, strPath)
  75.  
  76. Dim FileCount As Integer
  77. Dim myElement As System.Xml.XmlElement
  78. Dim strFileName As String
  79. Dim strNewFileName As String
  80. //復(fù)制引用的文件
  81. For Each myElement In myDoc.SelectNodes
    ("VisualStudioProject/CSHARP/Build/Referencds/Reference")
  82. strFileName = myElement.GetAttribute("HintPath")
  83. If System.IO.Path.IsPathRooted(strFileName) = False Then
  84. CopyFile(ThisPath, strPath, strFileName)
  85. FileCountFileCount = FileCount + 1
  86. End If
  87. Next
  88.  
  89. //復(fù)制項(xiàng)目文件
  90. For Each myElement In myDoc.SelectNodes
    ("VisualStudioProject/CSHARP/Files/Include/File")
  91. strFileName = myElement.GetAttribute("RelPath")
  92. If Not strFileName Is Nothing Then
  93. If System.IO.Path.IsPathRooted(strFileName) = False Then
  94. CopyFile(ThisPath, strPath, strFileName)
  95. FileCountFileCount = FileCount + 1
  96. DTE.StatusBar.Text = FileCount & " 正在復(fù)制文件 " & strFileName
  97. End If
  98. End If
  99. Next
  100. Return FileCount
  101. End Function
  102. //檢查當(dāng)前編輯的工程是不是C#工程
  103. Public Function CheckCSProject() As Boolean
  104. Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project
  105. If UCase(System.IO.Path.GetExtension(myPrj.FullName)) <> ".CSPROJ" Then
  106. MsgBox("當(dāng)前工程不是 C# 工程", MsgBoxStyle.Information, "系統(tǒng)提示")
  107. Return False
  108. End If
  109. Return True
  110. End Function
  111. //創(chuàng)建指定的目錄
  112. Public Sub CreateDirectory(ByVal strDir As String)
  113. If System.IO.Directory.Exists(strDir) = False Then
  114. System.IO.Directory.CreateDirectory(strDir)
  115. End If
  116. End Sub
  117. //將指定目錄下的指定相對路徑的文件復(fù)制到另一個目錄,保持相對路徑不變
  118. Public Sub CopyFile(ByVal strPath1 As String, 
    ByVal strPath2 As String, ByVal strFilePath As String)
  119. Dim strName1 As String = System.IO.Path.Combine(strPath1, strFilePath)
  120. Dim strName2 As String = System.IO.Path.Combine(strPath2, strFilePath)
  121. Dim dir1 As String = System.IO.Path.GetDirectoryName(strName1)
  122. If System.IO.Directory.Exists(dir1) = False Then
  123. System.IO.Directory.CreateDirectory(dir1)
  124. End If
  125. Dim dir2 As String = System.IO.Path.GetDirectoryName(strName2)
  126. If System.IO.Directory.Exists(dir2) = False Then
  127. System.IO.Directory.CreateDirectory(dir2)
  128. End If
  129. System.IO.File.Copy(strName1, strName2, True)
  130. End Sub
  131. //復(fù)制指定的文件到指定的目錄下
  132. Public Sub CopyFile(ByVal strFileName As String, ByVal strNewPath As String)
  133. System.IO.File.Copy(strFileName, System.IO.Path.Combine
    (strNewPath, System.IO.Path.GetFileName(strFileName)), True)
  134. End Sub

當(dāng)前題目:壓縮備份C#工程
文章分享:http://m.5511xx.com/article/cdgpcdi.html