在批处理文件中调用 vb.net 中的参数..?

Calling Parameters in vb.net within a batch file..?

我正在从批处理文件中启动我的 vb.net 控制台应用程序 .exe,例如:(用于测试目的的超时)

@Echo off
echo Starting Script
start C:\Users\user\Desktop\ConsoleApplication2
timeout /t 8 /nobreak
start C:\Users\user\Desktop\ConsoleApplication2

当vb.net控制台应用程序为运行时,进程名称、描述、date/time和计算机名称被传入并插入到sql数据库中。 然而,目前只有 date/time(在 sql 中完成)和计算机名称是正确的,因为它们是自动完成的,而名称和描述将由用户输入。我曾尝试至少为名称使用命令行参数,但由于 vb 应用程序是来自批处理文件的 运行ning,所以它没有用。所以现在我想知道是否有任何其他方法可以获得名称和描述输入(由于时间戳至关重要而无需更改 time/waiting 输入)或者是否有人有任何建议?

VB.net代码:

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Console = System.Console

Module Module1

Sub Main()
    'Connection to server
    Dim conn As New SqlClient.SqlConnection

    conn.ConnectionString = ("Server Connection Info...;")

    conn.Open()

    'Calling Stored Procedure
    Dim objCommand As New SqlClient.SqlCommand
    objCommand.Connection = conn
    objCommand.CommandText = "ProcessLog_Insert"
    objCommand.CommandType = CommandType.StoredProcedure

    'Pass in each of the required parameters
    objCommand.Parameters.Add("ProcessName", SqlDbType.NVarChar).Value = "Test3"
    'Pass in a description of the process being run
    objCommand.Parameters.Add("ProcessDescription", SqlDbType.NVarChar).Value = "Desc"
    'Pass in the Computer name used by current machine
    objCommand.Parameters.Add("ComputerName", SqlDbType.NVarChar).Value = Environment.MachineName
    'Execute the Query
    objCommand.ExecuteNonQuery()

    'Closes the Connection
    conn.Close()
    conn = Nothing
    objCommand = Nothing

End Sub
End Module

在批处理脚本中,%~f0 将 return 批处理文件的路径和文件名。启动应用程序时,使用 %~f0 将批处理文件名传递给 .exe。在您的 VB 应用程序中,您只需获取在命令行中传递的参数。

Finding out the file name of the running batch file

因此,如果我没理解错的话,描述可以作为参数传递到批处理文件中,批处理文件可以将描述和脚本名称传递给 vb 应用程序。

c:\temp\test.bat "My Lovely Description"

在你的批处理文件中:

c:\vbapp.exe "%1" "%~f0"

在 VB 中处理命令行参数非常简单,但如果您需要更多帮助,请相应地发表评论。