經過大量的在線研究,我得出以下結論,似乎工作得很好。 感謝大家對這個問題的任何意見。
首先打開一個新項目,將下面的代碼添加到按鈕中。 此代碼編譯您在將在下一步中創建的文本文件中編寫的代碼。
Private Sub CompilerButton_Click(sender As System.Object, e As System.EventArgs) Handles CompilerButton.Click
Dim objCodeCompiler As System.CodeDom.Compiler.ICodeCompiler = New VBCodeProvider().CreateCompiler() ' We create object of the compiler
Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters()
' Add reference
objCompilerParameters.ReferencedAssemblies.Add("System.dll")
objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
objCompilerParameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
'Compile in memory
Dim Output1 As String = "OutputApp"
objCompilerParameters.GenerateExecutable = True
objCompilerParameters.OutputAssembly = Output1
objCompilerParameters.CompilerOptions = "/target:winexe"
Dim strCode As String = My.Resources.TextFile1.ToString
Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = _
objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, strCode)
If objCompileResults.Errors.HasErrors Then
' If an error occurs
MsgBox("Error: Line>" & objCompileResults.Errors(0).Line.ToString & ", " & _
objCompileResults.Errors(0).ErrorText)
Exit Sub
End If
End Sub
然後在項目資源中添加一個文本文件並向它添加下面的代碼。 此代碼是您希望編譯爲獨立EXE的應用程序。你可以改變它的方式,你想要的。
Option Strict On
Imports System
Imports System.Windows.Forms
Imports System.Windows.Forms.Form
Imports Microsoft.VisualBasic
Namespace MyApp
Public Class EntryPoint
Public Shared Sub Main(args As [String]())
Dim FrmMain As New Form1
System.Windows.Forms.Application.Run(FrmMain)
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents Button1 As New Button
Sub New()
Application.EnableVisualStyles()
Me.Text = "Form1"
Button1.Text = "Click Me!"
Button1.Top = 100
Button1.Left = 100
Me.Controls.Add(Button1)
End Sub
Private Sub Button1_Click(Sender As Object, E As EventArgs) Handles Button1.Click
MsgBox("You Clicked Me!")
End Sub
End Class
End Namespace
如果你做了上述的一切,你點擊編譯它應該的名字OutputApp下創建的項目\ BIN \調試一個獨立的EXE之後。
再次感謝大家。 希望上面的代碼對任何試圖做同樣事情的人都有用。
我應該問爲什麼? – shanabus 2012-03-24 03:17:08
您可能想要節省一些頭痛的問題,並使用Visual Studio Visual Basic Express 2010而不是記事本。 http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-basic-express – mellodev 2012-03-24 03:17:36
@shanabus,我似乎沒有得到CodeDom的工作方式來編譯我的應用程序內的代碼,認爲做記事本是一種替代解決方案。 – NetInfo 2012-03-24 04:54:56