* VBNET 소스코드 동적 컴파일 예제...
전체 소스 코드
Form1.vb
Imports System.Runtime.InteropServices
Imports System.CodeDom.Compiler
Public Class Form1
Public Declare Function ShellExecuteA Lib "shell32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Integer) As IntPtr
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
TextBox1.Text = "Imports System " + vbCrLf + _
" Module Module1 " + vbCrLf + _
" Sub Main() " + vbCrLf + _
" System.Console.WriteLine(""Hello World!"")" + vbCrLf + _
" System.Console.WriteLine(""Press the Enter Key to Continue."")" + vbCrLf + _
" System.Console.ReadLine() " + vbCrLf + _
" End Sub " + vbCrLf + _
" End Module"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cdp As CodeDomProvider = CodeDomProvider.CreateProvider("vb")
Dim cp As CompilerParameters = New CompilerParameters()
'Flase : Dll output True : EXE File out put
cp.GenerateExecutable = True
'Path Set
cp.OutputAssembly = "C:\vbnettest.exe"
Dim cr As CompilerResults = cdp.CompileAssemblyFromSource(cp, TextBox1.Text)
If cr.Errors.Count > 0 Then
MessageBox.Show("Compile Error")
Return
End If
ShellExecuteA(Me.Handle, "open", "C:\vbnettest.exe", "", "", 4)
End Sub
End Class
*예제 결과
'VB.NET Programming' 카테고리의 다른 글
[VBNET] [WMI] CPU 클럭 속도 (CurrentClockSpeed) (0) | 2020.04.02 |
---|---|
[VBNET] 파일 비교 (File Compare) (0) | 2020.03.31 |
[VBNET] DateTimeFormat - 전역 설정 (0) | 2020.03.26 |
[VBNET] 설치된 닷넷프레임워크 버전 리스트 조회 (0) | 2020.03.24 |
[VBNET] [API] 제어판 기본 프린터(Default Printer) 변경 (0) | 2020.03.20 |