【问题标题】:command line arguments works in visual studio but not from command line for a WindowsForm project in Visual Basic命令行参数适用于 Visual Studio,但不适用于 Visual Basic 中的 WindowsForm 项目的命令行
【发布时间】:2023-03-30 20:45:01
【问题描述】:

我在 Visual Basic 中做了一个小程序。 它压缩文件并将文件上传到 ftp 服务器上。该程序本身运行良好。

我的问题是,如果它从命令行获取参数,我希望它有不同的行为。如果给定参数,则不显示任何表单,如果没有,则加载表单。

获取命令行参数的代码在从 Visual Basic 执行时工作正常(项目属性 -> 调试 -> 启动选项 -> 命令行参数设置为文件路径)

但是当我用这一行从 cmd 执行程序时: pogram.exe C:\Users\user\file.txt

显示表单,但未检测到任何参数。

这是调度的主要内容。
模块1.vb:

   Module Module1

Declare Function AttachConsole Lib "kernel32" (ByVal dwProcessId As Int32) As Boolean
Declare Function FreeConsole Lib "kernel32.dll" () As Boolean

Sub Main()
    Dim cmdArgs As String() = GetCommandLineArgs()
    Form2.loadData()
    If cmdArgs.Length = 0 Then ' NO ARGS MODE
        mainWindow.commandLineMode = False
        Application.Run(mainWindow)
    ElseIf cmdArgs.Length < 3 And Form2.TXTnumLicence.Text = "" Then ' BAD ARGS NUMBER
        Console.WriteLine("Aucun numero de licence definit. Usage : ./ODG_exchange ['filePath'] [numeroLicence [default = ./ODG_param.xml > numero_licence]]")
        Application.Exit()
    Else ' ARGS MODE
        mainWindow.commandLineMode = True
        mainWindow.commandeLine(cmdArgs)
    End If
End Sub

在 Visual Studio 中,我还将启动对象定义为 Module1。也许会有用。

【问题讨论】:

  • GetCommandLineArgsEnvironment.GetCommandLineArgs,对吗?
  • 这确实很可疑。还有“将启动对象定义为 Module1”,这是不可能的。这个问题有太多的出入。
  • Environment.GetCommandLineArgs 确实如此。我想可以设置启动对象有Module1,因为我这样做了。编辑:我仔细看了你为什么说这是不可能的,我在项目属性中禁用了应用程序框架复选框

标签: vb.net visual-studio-2010 command-line-arguments


【解决方案1】:

总有一个命令行参数——执行程序的文件名。 您需要将 if 语句更改为 cmdArgs.length = 1

【讨论】:

  • 确实如此。我之前以其他方式使用过 args,我修改了其余代码但忘记了这一行。我修复了它,但它仍然无法正常工作。感谢您的尝试
【解决方案2】:

7 天后我找到了解决方案...只需使用好的 .exe,它就可以在 Visual Studio 中运行...

我觉得很笨,谢谢你的帮助

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2022-01-19
    • 2011-03-28
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    相关资源
    最近更新 更多