【问题标题】:Calling Parameters in vb.net within a batch file..?在批处理文件中调用 vb.net 中的参数..?
【发布时间】:2015-04-20 14:35:47
【问题描述】:

我正在从批处理文件中启动我的 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 控制台应用程序运行时,进程名称、描述、日期/时间和计算机名称被传入并插入到 sql 数据库中。 然而,目前只有日期/时间(在 sql 中完成)和计算机名称是正确的,因为它们是自动完成的,而名称和描述将是用户输入。我尝试至少对名称使用命令行参数,但是看到 vb 应用程序是从批处理文件运行的,它不起作用。所以现在我想知道是否有任何其他方式可以获得名称和描述输入(不改变时间/等待输入,因为时间戳很重要)或者是否有人有任何建议?

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

【问题讨论】:

  • 那么,您是说用户将输入所需的信息,但是由于批处理文件执行了您的应用程序,您不能将这些内容保存到数据库并使用它吗?跨度>
  • 嗯,我想为应用程序放在批处理文件中的任何位置收集此信息,因为它将在许多脚本中使用。计算机名称和日期/时间很好,但我目前无法获得名称或描述,因为你不能让 vb 神奇地知道哪个 bat 文件正在运行它(这会给我名字) .所以是的,我目前无法获得这两个字段。
  • 根据所提供的信息,我们无法判断您使用什么数据进行描述,因此我们不确定如何提供帮助。描述来自哪里?如果用户在运行批处理文件时可以输入这些信息,那么就可以将其作为参数传递给批处理文件,然后批处理文件可以将其作为参数传递给vb应用程序。

标签: sql vb.net batch-file command-line-arguments


【解决方案1】:

在批处理脚本中,%~f0 将返回批处理文件的路径和文件名。启动应用程序时,使用 %~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 中处理命令行参数非常简单,但如果您需要更多帮助,只需相应地注释即可。

【讨论】:

  • 当我在 cmd 中输入不带括号的 (batfile.bat "hello") 时,我在同一行给出了文件的路径和 hello,我将如何合并 "hello" 或描述作为第二个参数?我不确定如何使用 %2 - %9 作为其他参数,我已经阅读了它们。Echo off C:\Users\user\Desktop\ConsoleApplication2 "%1" "%~f0" echo message timeout /t 1 /nobreak timeout /t 1 /nobreak echo message timeout /t 1 /nobreak C:\Users\user\Desktop\ConsoleApplication2 "%1" "%~f0" pause
  • 当批处理文件运行"C:\Users\user\Desktop\ConsoleApplication2 "%1" "%~f0" 时,它会将%1作为参数传递给vb应用程序,可以是用My.Application.CommandLineArgs(0)访问。批处理文件名,用“%~f0”传递,可以用My.Application.CommandLineArgs(1)访问。我不太明白你说的是什么它们“在同一行。”要查看发生了什么,请让您的 vb 应用程序中的第一个代码显示传入的两个参数。 MsgBox(My.Application.CommandLineArgs(0)) MsgBox(My.Application.CommandLineArgs (1))。
  • 然后运行批处理文件 c:\whatever\whatever.bat "MyDescription"。然后批处理文件将运行C:\Users\user\Desktop\ConsoleApplication2 "%1" "%~f0" ,它将“MyDescription”和批处理文件名传递给 vb 应用程序,它们可以作为 MsgBox(My.Application.CommandLineArgs(0)) 和 MsgBox(My.Application. CommandLineArgs(1)).
  • 啊哈! My.Application.CommandLineArgs(0) 正是我所需要的!至于描述,我可以用_作为空格将其全部写在1行中,然后再更改。非常感谢你帮助我~!
  • 很高兴我能帮上忙!请标记为答案,以便我对您的努力感到满意。 :)
猜你喜欢
  • 2020-06-05
  • 1970-01-01
  • 1970-01-01
  • 2013-03-01
  • 2015-02-26
  • 2017-12-31
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多