【发布时间】:2017-04-05 00:42:58
【问题描述】:
我需要创建一个 VB.NET 应用程序,它获取 Windows 窗体应用程序 的源代码并对其进行编译。我用这个link 作为参考。
我要创建的 Windows 窗体应用程序
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox("test")
End Sub
结束类
我的编译代码的 VB.NET 应用程序
Imports System.IO
Imports System.Reflection
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports Microsoft.VisualBasic
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim codeProvider As New VBCodeProvider()
Dim icc As ICodeCompiler = codeProvider.CreateCompiler
Dim Output As String = "Out.exe"
Dim ButtonObject As Button = CType(sender, Button)
textBox2.Text = ""
Dim parameters As New CompilerParameters()
Dim results As CompilerResults
'Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = True
parameters.OutputAssembly = Output
results = icc.CompileAssemblyFromSource(parameters, textBox1.Text)
If results.Errors.Count > 0 Then
'There were compiler errors
textBox2.ForeColor = Color.Red
Dim CompErr As CompilerError
For Each CompErr In results.Errors
textBox2.Text = textBox2.Text &
"Line number " & CompErr.Line &
", Error Number: " & CompErr.ErrorNumber &
", '" & CompErr.ErrorText & ";" &
Environment.NewLine & Environment.NewLine
Next
Else
'Successful Compile
textBox2.ForeColor = Color.Blue
textBox2.Text = "Success!"
'If we clicked run then launch the EXE
If ButtonObject.Text = "Run" Then Process.Start(Output)
End If
End Sub
Textbox1.text 包含我提供的第一个代码。当我运行程序时,它给了我这个错误
Line number 0, Error Number: BC30420, ''Sub Main' was not found in 'Out'.;
Line number 2, Error Number: BC30002, 'Type 'EventArgs' is not defined.;
Line number 3, Error Number: BC30451, ''MsgBox' is not declared. It may be inaccessible due to its protection level.;
Line number 3, Error Number: BC32017, 'Comma, ')', or a valid expression continuation expected.;
【问题讨论】:
-
开始一个新的 WinForms 项目。或者打开一个现有的 WinForms 项目。你在哪个部分有困难?什么版本的 Visual Studio?
-
@KenWhite 我知道如何编译项目、获取 exe 等。但是我如何编译 Windows 窗体应用程序。从它的源代码?
-
编译正在编译。您以完全相同的方式编译它们。再一次,哪个具体部分您遇到困难?我不应该问你 20 个问题来让你弄清楚问题。
-
@Plutonix:发帖人在你上面的评论中提供了链接。
-
我不能告诉你代码有什么问题,因为你的帖子没有代码。