DevExpress的.Net组件是功能较全的第三方组件,但其安装、发布都相对麻烦,重新编译涉及大量的项目更复杂。传统的方法是通过脚本来编译,需要了解项目的相互依存关系。
研究编译脚本,可理清思路,大体流程如下:
1、判断强签名文件是否存在,如不存在则生成。
2、从强签名文件中导出公钥。
3、判断项目中的AssemblyInfo.cs是否存在“PublicKey=”的字符串,如存在则替换。
4、按项目的依存关系编译。
从上面可以看出,步骤3和4的工作量最大。对于3可以写段代码解决,思路是通过递归查找所有的文件,然后用正则表达式实现替换。
Public Sub ReplacePublicKey(ByVal publicKeyToken As String) Dim ss() As String = Directory.GetFileSystemEntries(My.Application.Info.DirectoryPath) For i As Integer = 0 To ss.Length - 1 Dim s As String = ss(i) If Directory.Exists(s) Then GetFiles(s, publicKeyToken) ElseIf s.Contains("AssemblyInfo.cs") Then PatchFile(s, publicKeyToken) End If Next End Sub Private Sub GetFiles(ByVal path As String, ByVal publicKeyToken As String) Dim ss() As String = Directory.GetFileSystemEntries(path) For i As Integer = 0 To ss.Length - 1 Dim s As String = ss(i) If Directory.Exists(s) Then GetFiles(s, publicKeyToken) ElseIf s.Contains("AssemblyInfo.cs") Then PatchFile(s, publicKeyToken) End If Next End Sub Private Sub PatchFile(fileName As String, publicKeyToken As String) Dim s As String = "" Using reader As StreamReader = New StreamReader(fileName) s = reader.ReadToEnd() reader.Close() End Using If Not input.Contains("PublicKey") Then Exit Sub s = New Regex("\"", \s*PublicKey=[0123456789abcdefABCDEF]*\""").Replace(input, String.Format(""", PublicKey = {0}""", publicKeyToken)) Dim randomFileName As String = Path.GetRandomFileName() Using writer As StreamWriter = New StreamWriter(randomFileName) writer.Write(input) writer.Close() End Using File.Delete(fileName) File.Move(randomFileName, fileName) End Sub