Public Sub CreateCSProjectRAR()
If CheckCSProject() = False Then
Return
End If
Dim strPath As String
Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project
strPath = System.IO.Path.GetFileNameWithoutExtension(myPrj.FullName)
Dim strFileName As String
//设置要保存生成的文件的目录
strPath = System.IO.Path.Combine
("D:\SourceBack", strPath & "[" & System.DateTime.Now.ToString("yyyy-MM-dd") & "]")
strFileName = strPath & ".rar"
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
Dim iCount As Integer = CopyCSProjectFiles(strPath)
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
If iCount > 0 Then
DTE.StatusBar.Text = "正在生成压缩文件"
Dim start As New System.Diagnostics.ProcessStartInfo
//此处指定 WinRar 压缩软件的可执行文件名,若 WinRar安装在其他的目录则修改此文件名
start.FileName = "C:\Program Files\WinRAR\WinRAR.exe"
start.Arguments = "a -r -df -ep1 " & strFileName & " " & strPath
Dim p As SystemSystem.Diagnostics.Process = System.Diagnostics.Process.Start(start)
p.WaitForExit()
DTE.StatusBar.Text = "已生成压缩文件 " & strFileName
MsgBox("已生成压缩文件 " & strFileName, MsgBoxStyle.Information, "系统提示")
End If
End Sub
//将当前编辑的VS.NET2003的C#工程整体复制到用户指定的目录下,不支持VS.NET2005
Public Sub CopyCSProject()
//检查是否是C#工程
If CheckCSProject() = False Then
Return
End If
//让用户输入目录
Dim strPath As String = InputBox("请输入输出目录名称", "输入")
If strPath Is Nothing Then
Return
End If
If strPath.Length = 0 Then
Return
End If
//复制文件
Dim iCount As Integer = CopyCSProjectFiles(strPath)
MsgBox("共拷贝 " & iCount & " 个文件")
End Sub
//复制当前VS.NET2003的C#工程的所有包含的文件到指定的目录下,不支持VS.NET2005
//不复制项目中使用绝对路径引用的文件
Public Function CopyCSProjectFiles(ByVal strPath As String) As Integer
If CheckCSProject() = False Then
Return -1
End If
If System.IO.Directory.Exists(strPath) = False Then
System.IO.Directory.CreateDirectory(strPath)
End If
Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project
//加载项目文件
Dim myFile As New System.IO.StreamReader
(myPrj.FullName, System.Text.Encoding.GetEncoding(936))
Dim myDoc As New System.Xml.XmlDocument
myDoc.LoadXml(myFile.ReadToEnd())
myFile.Close()
Dim ThisPath As String = System.IO.Path.GetDirectoryName(myPrj.FullName)
//复制项目定义文件本身
CopyFile(myPrj.FullName, strPath)
Dim FileCount As Integer
Dim myElement As System.Xml.XmlElement
Dim strFileName As String
Dim strNewFileName As String
//复制引用的文件
For Each myElement In myDoc.SelectNodes
("VisualStudioProject/CSHARP/Build/Referencds/Reference")
strFileName = myElement.GetAttribute("HintPath")
If System.IO.Path.IsPathRooted(strFileName) = False Then
CopyFile(ThisPath, strPath, strFileName)
FileCountFileCount = FileCount + 1
End If
Next
//复制项目文件
For Each myElement In myDoc.SelectNodes
("VisualStudioProject/CSHARP/Files/Include/File")
strFileName = myElement.GetAttribute("RelPath")
If Not strFileName Is Nothing Then
If System.IO.Path.IsPathRooted(strFileName) = False Then
CopyFile(ThisPath, strPath, strFileName)
FileCountFileCount = FileCount + 1
DTE.StatusBar.Text = FileCount & " 正在复制文件 " & strFileName
End If
End If
Next
Return FileCount
End Function
//检查当前编辑的工程是不是C#工程
Public Function CheckCSProject() As Boolean
Dim myPrj As EnvDTE.Project = DTE.ActiveWindow.Project
If UCase(System.IO.Path.GetExtension(myPrj.FullName)) <> ".CSPROJ" Then
MsgBox("当前工程不是 C# 工程", MsgBoxStyle.Information, "系统提示")
Return False
End If
Return True
End Function
//创建指定的目录
Public Sub CreateDirectory(ByVal strDir As String)
If System.IO.Directory.Exists(strDir) = False Then
System.IO.Directory.CreateDirectory(strDir)
End If
End Sub
//将指定目录下的指定相对路径的文件复制到另一个目录,保持相对路径不变
Public Sub CopyFile(ByVal strPath2 As String,
ByVal strPath3 As String, ByVal strFilePath As String)
Dim strName1 As String = System.IO.Path.Combine(strPath2, strFilePath)
Dim strName2 As String = System.IO.Path.Combine(strPath3, strFilePath)
Dim dir1 As String = System.IO.Path.GetDirectoryName(strName1)
If System.IO.Directory.Exists(dir1) = False Then
System.IO.Directory.CreateDirectory(dir1)
End If
Dim dir2 As String = System.IO.Path.GetDirectoryName(strName2)
If System.IO.Directory.Exists(dir2) = False Then
System.IO.Directory.CreateDirectory(dir2)
End If
System.IO.File.Copy(strName1, strName2, True)
End Sub
//复制指定的文件到指定的目录下
Public Sub CopyFile(ByVal strFileName As String, ByVal strNewPath As String)
System.IO.File.Copy(strFileName, System.IO.Path.Combine
(strNewPath, System.IO.Path.GetFileName(strFileName)), True)
End Sub