NET控制台应用程序的源

NET控制台应用程序的源

本文介绍了如何编译使用VB.NET控制台应用程序的源$ C ​​$ C VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个VB.NET函数,它在源$ C ​​$一个VB.NET控制台应用程序的c和它编译成一个控制台应用程序

I need to create a VB.NET function that takes the source code of a VB.NET console application and compile it into a console application.

例如,这是VB.NET源$ C ​​$ C控制台应用程序:

For example, this is the VB.NET source code for the console application:

Module Module1

    Sub Main()
        Dim UserInfo As String = "Name: User1"

        System.Console.WriteLine(UserInfo)
        System.Console.ReadLine()
    End Sub

End Module

我的code到目前为止:

My code so far:

Friend Function CreateConsoleApplication(ByVal VBSourceCode As String, ByVal WhereToSave As String) As Boolean
    Try
        'now compile the source code contained in
        'VBSourceCode string variable

    Catch ex As Exception
        MessageBox.Show(ex.ToString)
        Return False
    End Try
End Function

更新:这是解决方案: -

UPDATE: Here is the solution:-

  Friend Function CreateConsoleApplication(ByVal VBSourceCode As String, ByVal WhereToSave As String) As Boolean
        Try

            VBSourceCode = "Module Module1" & vbCrLf & "Sub Main()" & vbCrLf & "Dim UserInfo As String = ""Name: User1""" & vbCrLf & "System.Console.WriteLine(UserInfo)" & vbCrLf & "System.Console.ReadLine()" & vbCrLf & "End Sub" & vbCrLf & "End Module"
            WhereToSave = "E:\TestConsole.exe"

            Dim provider As Microsoft.VisualBasic.VBCodeProvider
            Dim compiler As System.CodeDom.Compiler.ICodeCompiler
            Dim params As System.CodeDom.Compiler.CompilerParameters
            Dim results As System.CodeDom.Compiler.CompilerResults

            params = New System.CodeDom.Compiler.CompilerParameters
            params.GenerateInMemory = False

            params.TreatWarningsAsErrors = False
            params.WarningLevel = 4
            'Put any references you need here - even you own dll's, if you want to use one

            Dim refs() As String = {"System.dll", "Microsoft.VisualBasic.dll"}
            params.ReferencedAssemblies.AddRange(refs)
            params.GenerateExecutable = True
            params.OutputAssembly = WhereToSave

            provider = New Microsoft.VisualBasic.VBCodeProvider
            results = provider.CompileAssemblyFromSource(params, VBSourceCode)

            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
            Return False
        End Try
    End Function

好了,现在的code可以编译VB.NET源$ C ​​$ C到VB.NET控制台应用程序,非常感谢!但是,我们如何检查是否有这个结果有任何错误变量,我的意思是这行:的结果= provider.CompileAssemblyFromSource(参数,可以VBSource code)

Ok, now the code can compile VB.NET source code into VB.NET console application, thanks ! But how do we check if there is any error in this results variable, I mean this line: results = provider.CompileAssemblyFromSource(params, VBSourceCode)

推荐答案

过时的警告信息的通知的你如何避免接受它 - 使用中定义的方法的<一个href="http://msdn.microsoft.com/en-us/library/system.$c$cdom.compiler.$c$cdomprovider.compileassemblyfromsource.aspx"相对=nofollow> codeProvider 直接类,如:

The "Obsolete" warning message tells you how to avoid receiving it - use the method defined on the CodeProvider class directly, e.g.

provider = New Microsoft.VisualBasic.VBCodeProvider
'compiler = provider.CreateCompiler
results = provider.CompileAssemblyFromSource(params, VBSourceCode)

这篇关于如何编译使用VB.NET控制台应用程序的源$ C ​​$ C VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:02