vb.net VB中的shell命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8795723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:51:56  来源:igfitidea点击:

Shell commands in VB

vb.netvisual-studiovisual-studio-2010

提问by Nefariis

For some reason it seems that ampersands are not working like they should when I attempt to use them in shell commands in VB. When I attempt to link two commands together on the same line using an ampersand i receive the error: "filenotfoundexception was unhandled file not found"

出于某种原因,当我尝试在 VB 的 shell 命令中使用它们时,与符号似乎无法正常工作。当我尝试使用&符号在同一行上将两个命令链接在一起时,我收到错误:“filenotfoundexception was unhandled file not found”

The command I am trying to run is:

我试图运行的命令是:

 Shell("cd " & TextBox2.Text.ToString & " & adb -s " & TextBox15.Text.ToString & " shell monkey -p " & TextBox1.Text.ToString & " -v 1", AppWinStyle.Hide) 

I tried breaking it down to a more simplistic form, but im still receiving the error:

我尝试将其分解为更简单的形式,但我仍然收到错误消息:

 Shell("cd C:\ & adb shell monkey -p com.android.system -v 1", AppWinStyle.Hide)

If I get rid of the ampersand and just use:

如果我摆脱了&符号并使用:

 shell(adb shell monkey -p com.android.system -v 1", AppWinStyle.Hide)

everything works just fine. Are ampersands not available in vb shell commands?

一切正常。vb shell 命令中没有 & 符号吗?



*My edit

*我的编辑

Actually I am still having trouble. So what i have is:

其实我还是有问题。所以我所拥有的是:

    psi.WorkingDirectory = TextBox2.Text.ToString
    psi.FileName = "adb"
    psi.WindowStyle = ProcessWindowStyle.Hidden

then I have a little bit of code, and then I assign an argument and execute the argument:

然后我有一点代码,然后我分配一个参数并执行该参数:

    psi.Arguments = "-s " & TextBox15.Text.ToString & " shell monkey -p " & TextBox1.Text.ToString & " -v  1"
    Process.Start(psi)

then I have a little bit of code, and then I try running the process again with a different argument:

然后我有一些代码,然后我尝试使用不同的参数再次运行该过程:

    psi.Arguments = "-s " & TextBox15.Text.ToString & " shell input keyevent 3"
    Process.Start(psi)

First one seems to work, all the subsequent ones do not. Is there any reason why this shouldnt work? is there a process refresh or something that I am missing?

第一个似乎有效,所有后续的都不起作用。有什么理由为什么这不应该工作吗?是否有流程刷新或我缺少的东西?

回答by JohnFx

Deleted my other answer, found a simpler way to do this.

删除了我的其他答案,找到了一种更简单的方法来做到这一点。

This is what you want...

这就是你想要的...

Shell("cmd.exe /c cd C:\ & adb shell monkey -p com.android.system -v 1", AppWinStyle.Hide)

Inserting it into your original code...

将其插入到您的原始代码中...

Shell("cmd.exe /c cd " & TextBox2.Text.ToString & " & adb -s " & TextBox15.Text.ToString & " shell monkey -p " & TextBox1.Text.ToString & " -v 1", AppWinStyle.Hide) 

I tested the first example and it seemed to work.

我测试了第一个例子,它似乎有效。

回答by Paul Farry

Have you considered using the Process object to start ADB with the CommandLine options being set

您是否考虑过使用 Process 对象在设置了命令行选项的情况下启动 ADB

Dim psi As New ProcessStartInfo

psi.WorkingDirectory = "c:\"
psi.Arguments = "shell monkey -p com.android.system -v 1"
psi.FileName = "ADB"
psi.WindowStyle = ProcessWindowStyle.Hidden
return Process.Start(psi)

in the event that your ADB program only allows a single instance to run, maybe you need to add the following

如果您的 ADB 程序只允许运行单个实例,您可能需要添加以下内容

Dim ps As Process = Process.Start(psi)
ps.WaitForExit()

psi.Arguments = 'new arguments
Process.Start(psi)

回答by competent_tech

The Shell command expects a file name, so command line extensions won't work.

Shell 命令需要一个文件名,因此命令行扩展名不起作用。

There are a couple of options:

有几个选项:

1) Start cmd.exe with process.start and pass the parameters (I have not tested this, so am unsure if it will work.

1)用 process.start 启动 cmd.exe 并传递参数(我没有测试过这个,所以不确定它是否可以工作。

2) Create your commands in a .cmd or .bat file and then shell that file (this seems like it might be the easiest approach).

2) 在 .cmd 或 .bat 文件中创建您的命令,然后 shell 该文件(这似乎可能是最简单的方法)。