高分求教,一个vb.net中非常有趣的问题

bailen 2008-12-10 11:04:02
在vb.net中我使用Environment.GetCommandLineArgs 或者其他什么方法,怎么读到启动时的参数,例如:程序名为test.exe,在生成的这个程序后加入以下参数:
test.exe /u<userid> /p<password> /g<group>
我现在用什么方法可以分别读到userid,password,group,具体怎么实现,请各位指教
...全文
167 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
RexZheng 2008-12-10
  • 打赏
  • 举报
回复


Private Function GetCommandLineArg(ByVal param As String) As String
Dim args As String() = Environment.GetCommandLineArgs()
Dim index As Integer = Array.IndexOf(args, param)
If index > -1 AndAlso index < args.Length - 2 Then
Return args(index + 1)
Else
Return Nothing
End If
End Function




调用:


Dim password As String = GetCommandLineArg("/p")

bailen 2008-12-10
  • 打赏
  • 举报
回复
不是很明白,能否根据我上面的例子,用vb.net写一段给我看看,非常感谢
RexZheng 2008-12-10
  • 打赏
  • 举报
回复


好像不提供这种参数检索方法,只能自己封装吧。


string GetCommandLineArg(string param)
{
string[] args = Environment.GetCommandLineArgs();
int index = Array.IndexOf(args, param);
if (index > -1 && index < args.Length - 2)
{
return args[index + 1];
}
else
{
return null;
}
}
bw555 2008-12-10
  • 打赏
  • 举报
回复
给main函数加入参数
Sub Main(ByVal CmdArgs() As String)
Dim userid,password,group As String
Dim ArgNum As Integer
userid= CmdArgs(0)
password= CmdArgs(1)
group = CmdArgs(2)
end sub
yanlongwuhui 2008-12-10
  • 打赏
  • 举报
回复
Dim args As String() = System.Environment.GetCommandLineArgs()
For Each arg As String In args
'......
Next
fairy4 2008-12-10
  • 打赏
  • 举报
回复
帮顶
yanlongwuhui 2008-12-10
  • 打赏
  • 举报
回复
up
水滴 2008-12-10
  • 打赏
  • 举报
回复
使用My.Application.CommandLineArgs可以将命令行参数读到你想要的字符串便量中,代码如下:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim CmdStr() As String

If My.Application.CommandLineArgs.Count <> 0 Then
ReDim CmdStr(My.Application.CommandLineArgs.Count - 1)
For i As Integer = 0 To My.Application.CommandLineArgs.Count - 1
CmdStr(i) = My.Application.CommandLineArgs(i)
Next
End If
End Sub
haifeng39 2008-12-10
  • 打赏
  • 举报
回复
帮顶
oo渣渣oo 2008-12-10
  • 打赏
  • 举报
回复
取到内容后自己再来解析
oo渣渣oo 2008-12-10
  • 打赏
  • 举报
回复
这样就行了:

Sub Main
Dim theCommand As String = Command()
End Sub
bailen 2008-12-10
  • 打赏
  • 举报
回复
没有读到,是不是要在那里设置命令行参数:)谢谢上面的兄弟

16,549

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧