2017-01-09 2 views
0

"cmdGenerate"버튼이있는 앱이 있습니다. 내가 원하는 것은, 그 버튼을 클릭 할 때 콘솔 응용 프로그램을 특정 코드와 함께 만들어야한다는 것입니다. 생성 될 필요가 콘솔 응용 프로그램이이 기능을 가지고 예를 들어VB.NET을 사용하여 .exe 파일을 동적으로 생성

:

Dim WIDTH as integer = someValuePlacedWhenGeneratingFile 
    Dim HEIGHT as integer = someValuePlacedWhenGeneratingFile 

    Function calculateArea(x as Integer, y as Integer) as Integer 
     return x*y 
    End Function 

someValuePlacedWhenGeneratingFile 내가 파일을 생성 할 때 변경할 것입니다. 그게 가능하니?

생성 된 EXE 파일은 내 주 발전기 응용 프로그램에서 설정 한 것 값으로 위의 하나 WIDTH와 HEIGHT 세트와 같은 기능을 가진 단지 콘솔 응용 프로그램이어야한다.

+2

가능한 복제 [? 동적으로 소스 코드에서 컴파일 된 .NET exe 인을 만들] (http://stackoverflow.com/questions/3591587/ 소스 코드에서 동적으로 작성 - 컴파일 된 - net-exe) – GSerg

+1

@GSerg에서 언급 한 복제본을 사용하십시오. "CSharp"를 "VisualBasic"(하나의 대답)으로 대체하거나 "csc"를 "vbc"로 대체하십시오. –

답변

0

누구나 여기에 필요한 경우에는 내가 어떻게했는지에 대한 전체 예가 나와 있습니다.

는 ("txtCode"라는 이름의)를 텍스트 상자버튼 (이름 "cmdGenerate")와 양식을 만듭니다.

- 작동 방식 : 생성 된 .exe에 포함될 코드를 입력 한 다음 cmbButton을 누릅니다. 그 후, .exe 파일이 생성되고 실행되면 다른 응용 프로그램과 마찬가지로 명령이 실행됩니다. 당신이 txtCode 텍스트 상자 내부 코드를을 writting 마친 후

Public Sub generateFile() 

    Dim codeProvider As New VBCodeProvider() 
    Dim icc As ICodeCompiler = codeProvider.CreateCompiler 
    Dim Output As String = "C:\Program Files\MyApp.exe" 


    Dim parameters As New CompilerParameters() 
    Dim results As CompilerResults 

    'Make sure we generate an EXE, not a DLL 
    parameters.GenerateExecutable = True 
    parameters.OutputAssembly = Output 
    parameters.CompilerOptions = ("/target:winexe" & " " & "/win32icon:" & """") & "C:\Program Files\MyIcons\Icon.ico" & """" 

    results = icc.CompileAssemblyFromSource(parameters, txtCode.Text) 

    If results.Errors.Count > 0 Then 
     'There were compiler errors 
     Dim CompErr As CompilerError 
     For Each CompErr In results.Errors 
      MessageBox.Show(
       "Line number " & CompErr.Line & 
       ", Error Number: " & CompErr.ErrorNumber & 
       ", '" & CompErr.ErrorText & ";" & 
       Environment.NewLine & Environment.NewLine) 
     Next 
    Else 

     'Successfull Compile 
     MessageBox.Show("Your file is successfully generated!", "Success") 
    End If 

End Sub 

그리고, 그냥 그 메소드를 호출

파일 생성을위한 방법이다.

txtCode 내부 이동 코드 예제는 다음과 같습니다

Imports System 
Imports System.Diagnostics 

Module Module1 

    Sub Main() 

     Dim CmdStr As String = "CMD ARGUMENTS----" 
     Dim startInfo As New ProcessStartInfo("CMD.EXE") 
     startInfo.WindowStyle = ProcessWindowStyle.Minimized 
     startInfo.WindowStyle = ProcessWindowStyle.Hidden 
     startInfo.CreateNoWindow = True 
     startInfo.UseShellExecute = False 
     startInfo.Arguments = CmdStr 
     Process.Start(startInfo) 

    End Sub 

End Module