我有一个带按钮的应用程序" cmdGenerate"。我想要的是,当我点击该按钮时,它需要创建一个控制台应用程序,其中包含特定的代码。
例如,需要生成的控制台应用程序具有以下功能:
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 设置为我在主生成器应用程序中设置的值
答案 0 :(得分:0)
如果有人需要这个,那就是我如何做到这一点的完整例子。
使用文本框(名为&#34; txtCode&#34;)和按钮(名称&#34; cmdGenerate&#34;)创建表单。< / p>
- 如何工作:输入生成的.exe包含的代码,然后按cmbButton。之后,生成文件.exe并在启动时执行命令,就像任何其他应用程序一样。
这是文件生成的方法:
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文本框中编写代码之后,只需调用该方法。
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