.net调用cmd后无法输入命令

ztmsimon 2016-02-02 04:59:26


为什么我打开cmd后这里什么东西都没有?往里面也写不了内容啊?
有没大神救一下啊~!

附上我的代码:

Public Function ExeCommand(ByVal commandText As String) As Boolean
Dim p As New Process

p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
'p.StartInfo.CreateNoWindow = True
Try
p.Start()
p.StandardInput.AutoFlush = True
p.StandardInput.WriteLine("command:" & commandText)
p.StandardInput.WriteLine("exit")
p.WaitForExit()
p.Close()
Catch ex As Exception
Return False
End Try

Return True
...全文
365 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ztmsimon 2016-02-03
  • 打赏
  • 举报
回复
我现在代码改成
p.StartInfo.FileName = "cmd.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardInput = True
Try
p.Start()
p.StandardInput.WriteLine(commandText)
p.StandardInput.WriteLine("exit")
p.WaitForExit()
p.Close()
Catch ex As Exception
Return False
End Try

运行到"p.Start()"的时候cmd里面提示句柄无效,然后也无法输入内容。


ztmsimon 2016-02-03
  • 打赏
  • 举报
回复


现在cmd里面有内容了,但是运行到"p.StandardInput.WriteLine(commandText)"
这行代码的时候还是报错。
Justin-Liu 2016-02-02
  • 打赏
  • 举报
回复
把烂糟的属性设置都去掉,直接运行cmd.exe
Poopaye 2016-02-02
  • 打赏
  • 举报
回复
输入输出都重定向了,当然没东西了 要么就把这句去掉p.StartInfo.RedirectStandardOutput = True

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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