首先要保证电脑中要安装有.Net Framework
他的位置在C:\Windows\Microsoft.NET\Framework\版本号
目前最常用的版本是.Net4.0,也是xp可以支持的最高版本,同时也是win10自带的版本,而且很多软件也都需要使用该运行库,所以基本每个电脑都有安装。
在C:\Windows\Microsoft.NET\Framework\v4.0.30319 的文件夹目录下很多文件,其中 csc 为C#(csharp)编译器,vbc为vbNet编译器。
在控制台中输入vbc可以获得帮助
Microsoft (R) Visual Basic Compiler version 14.7.3056 for Visual Basic 2012 Copyright (c) Microsoft Corporation. All rights reserved. This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to Visual Basic 2012, which is no longer the latest version. For compilers that support newer versions of the Visual Basic programming language, see http://go.microsoft.com/fwlink/?LinkID=533241 Visual Basic Compiler Options - OUTPUT FILE - /out:<file> Specifies the output file name. /target:exe Create a console application (default). (Short form: /t) /target:winexe Create a Windows application. /target:library Create a library assembly. /target:module Create a module that can be added to an assembly. /target:appcontainerexe Create a Windows application that runs in AppContainer. /target:winmdobj Create a Windows Metadata intermediate file /doc[+|-] Generates XML documentation file. /doc:<file> Generates XML documentation file to <file>. - INPUT FILES - /addmodule:<file_list> Reference metadata from the specified modules. /link:<file_list> Embed metadata from the specified interop assembly. (Short form: /l) /recurse:<wildcard> Include all files in the current directory and subdirectories according to the wildcard specifications. /reference:<file_list> Reference metadata from the specified assembly. (Short form: /r) - RESOURCES - /linkresource:<resinfo> Links the specified file as an external assembly resource. resinfo:<file>[,<name>[,public|private]] (Short form: /linkres) /nowin32manifest The default manifest should not be embedded in the manifest section of the output PE. /resource:<resinfo> Adds the specified file as an embedded assembly resource. resinfo:<file>[,<name>[,public|private]] (Short form: /res) /win32icon:<file> Specifies a Win32 icon file (.ico) for the default Win32 resources. /win32manifest:<file> The provided file is embedded in the manifest section of the output PE. /win32resource:<file> Specifies a Win32 resource file (.res). - CODE GENERATION - /optimize[+|-] Enable optimizations. /removeintchecks[+|-] Remove integer checks. Default off. /debug[+|-] Emit debugging information. /debug:full Emit full debugging information (default). /debug:pdbonly Emit PDB file only. - ERRORS AND WARNINGS - /nowarn Disable all warnings. /nowarn:<number_list> Disable a list of individual warnings. /warnaserror[+|-] Treat all warnings as errors. /warnaserror[+|-]:<number_list> Treat a list of warnings as errors. - LANGUAGE - /define:<symbol_list> Declare global conditional compilation symbol(s). symbol_list:name=value,... (Short form: /d) /imports:<import_list> Declare global Imports for namespaces in referenced metadata files. import_list:namespace,... /langversion:<number> Specify language version: 9|10|11. /optionexplicit[+|-] Require explicit declaration of variables. /optioninfer[+|-] Allow type inference of variables. /rootnamespace:<string> Specifies the root Namespace for all type declarations. /optionstrict[+|-] Enforce strict language semantics. /optionstrict:custom Warn when strict language semantics are not respected. /optioncompare:binary Specifies binary-style string comparisons. This is the default. /optioncompare:text Specifies text-style string comparisons. - MISCELLANEOUS - /help Display this usage message. (Short form: /?) /noconfig Do not auto-include VBC.RSP file. /nologo Do not display compiler copyright banner. /quiet Quiet output mode. /verbose Display verbose messages. - ADVANCED - /baseaddress:<number> The base address for a library or module (hex). /bugreport:<file> Create bug report file. /codepage:<number> Specifies the codepage to use when opening source files. /delaysign[+|-] Delay-sign the assembly using only the public portion of the strong name key. /errorreport:<string> Specifies how to handle internal compiler errors; must be prompt, send, none, or queue (default). /filealign:<number> Specify the alignment used for output file sections. /highentropyva[+|-] Enable high-entropy ASLR. /keycontainer:<string> Specifies a strong name key container. /keyfile:<file> Specifies a strong name key file. /libpath:<path_list> List of directories to search for metadata references. (Semi-colon delimited.) /main:<class> Specifies the Class or Module that contains Sub Main. It can also be a Class that inherits from System.Windows.Forms.Form. (Short form: /m) /moduleassemblyname:<string> Name of the assembly which this module will be a part of. /netcf Target the .NET Compact Framework. /nostdlib Do not reference standard libraries (system.dll and VBC.RSP file). /platform:<string> Limit which platforms this code can run on; must be x86, x64, Itanium, arm, AnyCPU32BitPreferred or anycpu (default). /sdkpath:<path> Location of the .NET Framework SDK directory (mscorlib.dll). /subsystemversion:<version> Specify subsystem version of the output PE. version:<number>[.<number>] /utf8output[+|-] Emit compiler output in UTF8 character encoding. @<file> Insert command-line settings from a text file. /vbruntime[+|-|*] Compile with/without the default Visual Basic runtime. /vbruntime:<file> Compile with the alternate Visual Basic runtime in <file>.
以上就是vb编译器的参数,但是参数不是全都需要用到的,只需要选择自己使用的就可以了。
我要做一个启动其他程序的小工具,但是希望可以让别人更改启动的其他程序或者这个小工具的图标,那么就可以使用这种方便的操作。
另外,很多人都觉得奇怪,VS建立的不都是工程项目吗,总不能把工程文件都放进去吧,单独的代码文件缺东西没办法编译啊。但是仔细看工程目录可以发现,AssemblyInfo、Settings.Designer、Resources.Designer,这些自动生成的文件,还有窗体中的Designer设计器文件,后缀名也都是.cs或者.vb,都是普通的代码文件,所以可以把它们塞到一个文件中去。
Imports System Imports System.Reflection Imports System.Runtime.InteropServices <Assembly: AssemblyTitle("GameLauncher")> <Assembly: AssemblyDescription("GameLauncher")> <Assembly: AssemblyCompany("JomiXedYu")> <Assembly: AssemblyProduct("GameLauncher")> <Assembly: AssemblyCopyright("Copyright © 2019 JomiXedYu")> <Assembly: AssemblyTrademark("")> <Assembly: ComVisible(False)> <Assembly: Guid("127200de-9eee-430b-b6cb-2fb41977d8fd")> <Assembly: AssemblyVersion("1.0.0.0")> <Assembly: AssemblyFileVersion("1.0.0.0")> Module Launcher Dim launchProgramName As String = "BlueTailAVGEngine.exe" Sub main() Dim AppPath As String = My.Application.Info.DirectoryPath Dim FullPath As String = AppPath + "\" + launchProgramName If (launchProgramName <> "" And System.IO.File.Exists(FullPath)) Then System.Diagnostics.Process.Start(FullPath) Else MsgBox("Error.未找到该文件") End If End Sub End Module
这个就是一个独立的.vb文件,想让文件拥有介绍的元数据,所以把AssemblyInfo中的内容放到了这里,然后通过模块中的主入口Sub main运行。
然后新建一个Compile.cmd或者Compile.bat都可以,写入调用vbc编译的命令
@ECHO OFF CLS echo Compile... if Not exist ".\icon.ico" ( echo "[ERROR]^>file icon.ico could not be found." pause exit ) if exist "Launcher.exe" ( del ".\Launcher.exe" echo "DeleteFile Launcher.exe" ) echo /t:winexe /win32icon:%~dp0\icon.ico %~dp0\Launcher.vb cd %WINDIR%\Microsoft.NET\Framework\v4.0.30319 vbc /t:winexe /win32icon:%~dp0\icon.ico %~dp0\Launcher.vb echo Done! pause
最先判断了一个叫icon.ico的图标文件,这样可以让其他人直接更换图标文件运行此脚本就可以重新编译了(程序元数据需要通过代码文件更改)
主要命令就两条
cd %WINDIR%\Microsoft.NET\Framework\v4.0.30319 vbc /t:winexe /win32icon:%~dp0\icon.ico %~dp0\Launcher.vb
cd不用说什么,%WINDIR%是windows文件夹的变量,如果你的windows装在D盘,%WINDIR%就是D:\Windows
vbc中 /t 代表target 目标的意思,也就是链接目标是exe还是dll之类的,所以/t:winexe是编译exe
/win32icon:%~dp0\icon.ic,这个参数如果不加就不会编译图标进去,会使用windows默认的程序图标。%~dp0代表当前的的路径。
最后这个%~dp0\Launcher.vb也就是代码文件了,如果有多个代码文件用逗号分割。
保存该脚本文件运行后就会编译到当前目录
文章评论