代码动态编译的一些问题
各位大虾,小弟最近要使用动态编译生成exe程序,可是发现生成的exe是控制台程序而不是windows窗体应用程序(有用户交互界面的),这样有些用户交互功能无法实现(比如说显示一个窗体等),不知道哪里可以设置生成的应用程序类型的?
Public Shared Sub CreateExe(ByVal exeName As String, ByVal Code As String)
'创建一个编译器.
Dim Comp As VBCodeProvider = New VBCodeProvider()
Dim Parms As CompilerParameters = New CompilerParameters
Parms.GenerateExecutable = True '创建exe文件而不是dll
Parms.OutputAssembly = exeName '产生的应用程序名
Parms.TreatWarningsAsErrors = False'编译器将不会将警告视为错误。
'添加你想引用的所有集合。
Parms.ReferencedAssemblies.Add("System.Windows.Forms.dll")
Parms.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
Parms.ReferencedAssemblies.Add("System.dll")
Parms.ReferencedAssemblies.Add("mscorlib.dll")
'定义你要运行的代码。
Dim Executable As CompilerResults = Comp.CompileAssemblyFromSource(Parms, Code)
'如果再有任何错误消息显示。
If Executable.Errors.HasErrors Then
For Each Item As CompilerError In Executable.Errors()
MessageBox.Show(Item.ErrorText)
Next
Else '如果再没有任何错误信息,启动可执行文件。
Process.Start(exeName)
End If
End Sub
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System
Imports System.Diagnostics
Public Class C_Text
Private Shared Timer As New Timer
Private Shared TimerInterval As Integer = 5000
Public Shared Sub Main()
AddHandler Timer.Tick, AddressOf Timer_Tick
Timer.Interval = TimerInterval
Timer.Start()
End Sub
Private Shared Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
'这里是timer的tick事件,具体代码就不写了
End Sub
End Class
Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Namespace Text
Public Class C_Text
Public Shared Sub Main()
Application.Run(New MainFrm)
End Sub
End Class
Public Class MainFrm
Inherits Windows.Forms.Form
Private Shared Button1 As Button
Public Sub New()
Button1.Text = "Text"
Button1.Top = 100 : Button1.Left = 100
AddHandler Button1.Click, AddressOf Button1_Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Text")
End Sub
End Class
End Namespace