VB.net中充分利用CMD里的命令问题

gxpang 2005-05-24 11:46:19

VB.net中充分利用CMD里的命令

怎么让 VB.net把 CMD 集成在软件里呢

如 用 Shell 执行 chkdsk C; 并不是出现 CMD窗口,而是在 VB.net设计的程序中出现(如优化大师

中的 “系统磁盘医生”一样)

这样是怎么实现的呢?

请各位高手帮帮我啊,偶先谢谢了!!
...全文
547 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ninputer 2005-06-06
  • 打赏
  • 举报
回复
不要将CMD的执行窗口嵌到你的窗口中啊,明显的非正常思路……
Ninputer 2005-06-06
  • 打赏
  • 举报
回复
你把cmd的输出重定向到你的一个Stream中,然后就可以进行任何判断啊,显示啊,操作啊什么的了,多好呀。
gxpang 2005-06-05
  • 打赏
  • 举报
回复

谢谢 AntingZ(夕惕若)

可输入的命令没有执行啊,还有再输入命令时,CMD窗口就增多!???


问一下有没有 办法检测得到
Private Sub AppText(ByVal strAdd As String)
...
Me.TextBox1.Text &= strAdd

End Sub

中的 strAdd 字符是否有 “完成了百分之” 的 出现,且不会影响到程序 的运行 ???
sz_lgp 2005-06-04
  • 打赏
  • 举报
回复
up
AntingZ 2005-06-03
  • 打赏
  • 举报
回复
当然,上面的比较粗糙,你可以通过其他API来改善效果
比如用SetWindowPos来设置CMD窗口的位置,大小等。
AntingZ 2005-06-03
  • 打赏
  • 举报
回复
没见过优化大师,下面的代码应该可以吧CMD窗体嵌套到你自己的窗体内:
Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Diagnostics.Process.Start("c:\windows\system32\cmd.exe")
System.Threading.Thread.Sleep(50) '我发现,不休息的话,下面的FindWindow有可能找不到窗体
Dim h As IntPtr
h = FindWindow(vbNullString, "c:\windows\system32\cmd.exe")

SetParent(h, Me.Panel1.Handle) '嵌套到panel1内

'SetParent(h, Me.Handle) '或者直接嵌套到窗体内
End Sub
elining 2005-06-03
  • 打赏
  • 举报
回复
帮你顶~
不过估计这个你要到MS上去问,毕竟还是有些微软的程序员在逛那里!
gxpang 2005-05-31
  • 打赏
  • 举报
回复

不知还该不该顶???
gxpang 2005-05-29
  • 打赏
  • 举报
回复

我再顶??????????????????????????????
gxpang 2005-05-28
  • 打赏
  • 举报
回复
elining(萧)


---------------------
其实我是在 http://blog.csdn.net/kissnono/archive/2005/03/29/332965.aspx 这copy 来改一下下而已,只不过有些功能没有实现罢了,请各位高手看能不能 让输入 chkdsk 没有多重复的

“完成了百分之” 出现!???????
elining 2005-05-28
  • 打赏
  • 举报
回复
楼主,你说的问题我看不懂(水平还没那么高)

但我也提几个我认为的问题:

1.你写的东东,窗口是不会自动在最下面,或是说,我输入任何命令后,窗口滚动条内容还是在上面,要手动把滚动条拖下来才看到.

2.回车时,系统老是"当当"响,好难听喔,希望你定义一个声音进去!
gpanxcc 2005-05-28
  • 打赏
  • 举报
回复

、嗯!??!?!??!!
gxpang 2005-05-27
  • 打赏
  • 举报
回复
下面是写入的代码

Public Shared Sub Main()
If System.Environment.OSVersion.ToString.IndexOf("NT") = -1 Then
MsgBox("暂时不支持非NT系统,程序退出!")
Application.Exit()
Else
Application.Run(New Form1)
End If
End Sub


#Region "private viable"
Dim sw As IO.StreamWriter
Dim sr As New MyStreamReader
Dim err As New MyStreamReader
Dim p As System.Diagnostics.Process = New System.Diagnostics.Process
Dim psI As New System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("ComSpec"))
#End Region

Friend Class MyStreamReader
Public stream As IO.StreamReader
Public bytes(1024) As Byte
Public Sub New()
End Sub
End Class


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
p = New System.Diagnostics.Process
'"cmd.exe"为nt的命令行程序
psI = New System.Diagnostics.ProcessStartInfo("cmd.exe") '指定启动进程时用的一组数值
psI.UseShellExecute = False '是否使用外壳程序启动进程
psI.RedirectStandardInput = True '从文件读入数据
psI.RedirectStandardOutput = True '向文件写入数据
psI.RedirectStandardError = True '用于向文件或日志写入数据
psI.CreateNoWindow = True '是否在新窗口中启动该进程值

p.StartInfo = psI
p.Start()
sw = p.StandardInput '取一个流,用于写入应用程序输入
sr.stream = p.StandardOutput '取一个流,用于读取应用程序输出
err.stream = p.StandardError '取用于读取应用程序错误输出的流
sw.AutoFlush = True '是否在每次调用时,从缓冲流刷新到基础流
sr.stream.BaseStream.BeginRead(sr.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), sr)
err.stream.BaseStream.BeginRead(err.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), err)

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Delegate Sub TextAddHandler(ByVal strPara As String)

Private Sub AppText(ByVal strAdd As String)
Dim sums As Object '用于传递
Dim flag As Boolean = False
Dim i As Object

Me.TextBox1.Text &= strAdd

End Sub

'回调函数
'为了保证执行invoke方法的线程顺序,使用了MethodImpl
<MethodImpl(MethodImplOptions.Synchronized)> _
Sub CBstream(ByVal s As IAsyncResult)
Try
Dim t As MyStreamReader = CType(s.AsyncState, MyStreamReader)
If t.stream.BaseStream Is Nothing Then
Exit Sub
End If
Dim i As Integer = t.stream.BaseStream.EndRead(s)
Dim strReceive As String = System.Text.Encoding.Default.GetString(t.bytes, 0, i)
Me.Invoke(New TextAddHandler(AddressOf AppText), New Object() {strReceive})
t.stream.BaseStream.BeginRead(t.bytes, 0, 1024, New AsyncCallback(AddressOf CBstream), t)
Catch ex As Exception
MsgBox("CBstream--" & ex.ToString)
End Try
End Sub

Private Sub Bt执行_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt执行.Click
Me.CmdExcute()
End Sub

Private Sub CmdExcute()
Try
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
If Me.ComboBox1.Text <> "" Then
sw.WriteLine(Me.ComboBox1.Text)
Else
sw.WriteLine("Dir")
End If
Me.Cursor = System.Windows.Forms.Cursors.Default

'向combobox中添加元素
' Me.ComboBox1.Items.Add(Me.ComboBox1.Text)
' Me.ComboBox1.SelectionStart = 0
' Me.ComboBox1.SelectionLength = Me.ComboBox1.Text.Length
Me.ComboBox1.Focus()
Catch ex As Exception
MsgBox("CmdExcute--" & ex.ToString)
End Try
End Sub

Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Try

p.Close()
If Not sw Is Nothing Then
sw.Close()
End If
If Not sr Is Nothing Then
sr.stream.Close()
End If
If Not err Is Nothing Then
err.stream.Close()
End If

Catch ex As Exception
End
End Try
End
End Sub


Private Sub Bt清空_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt清空.Click
Me.TextBox1.Text = String.Empty
End Sub

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then '是否按下回车键
Me.CmdExcute()
End If
End Sub
End Class



------------------------------------------------------------------
 有人帮我改一下下代码么,意思是说,当输入 chkdsk 时就出现问题了,是会不断重复 “完成了百分之”
也就是说会有一百个 “完成了百分之 ” 出现 应该怎么解决啊????????????
gpanxcc 2005-05-27
  • 打赏
  • 举报
回复

...........
gxpang 2005-05-27
  • 打赏
  • 举报
回复

Imports System.Threading
Imports System.Runtime.CompilerServices
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()
Application.EnableVisualStyles()
Application.DoEvents()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Bt执行 As System.Windows.Forms.Button
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents Bt清空 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Panel
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.Label1 = New System.Windows.Forms.Label
Me.Bt执行 = New System.Windows.Forms.Button
Me.Bt清空 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Panel1.Controls.Add(Me.ComboBox1)
Me.Panel1.Controls.Add(Me.Label1)
Me.Panel1.Controls.Add(Me.Bt执行)
Me.Panel1.Controls.Add(Me.Bt清空)
Me.Panel1.Location = New System.Drawing.Point(8, 352)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(560, 48)
Me.Panel1.TabIndex = 1
'
'ComboBox1
'
Me.ComboBox1.Location = New System.Drawing.Point(216, 16)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(176, 20)
Me.ComboBox1.TabIndex = 3
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(152, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 24)
Me.Label1.TabIndex = 2
Me.Label1.Text = "命令"
'
'Bt执行
'
Me.Bt执行.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Bt执行.Location = New System.Drawing.Point(440, 16)
Me.Bt执行.Name = "Bt执行"
Me.Bt执行.Size = New System.Drawing.Size(64, 24)
Me.Bt执行.TabIndex = 0
Me.Bt执行.Text = "执行(&E)"
'
'Bt清空
'
Me.Bt清空.FlatStyle = System.Windows.Forms.FlatStyle.System
Me.Bt清空.Location = New System.Drawing.Point(32, 16)
Me.Bt清空.Name = "Bt清空"
Me.Bt清空.Size = New System.Drawing.Size(72, 24)
Me.Bt清空.TabIndex = 0
Me.Bt清空.Text = "清空"
'
'TextBox1
'
Me.TextBox1.BackColor = System.Drawing.Color.Black
Me.TextBox1.ForeColor = System.Drawing.Color.White
Me.TextBox1.Location = New System.Drawing.Point(16, 16)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ReadOnly = True
Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox1.Size = New System.Drawing.Size(552, 320)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(576, 406)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Panel1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "实现CMD功能"
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region
-----------------
这是自动生成的
gxpang 2005-05-26
  • 打赏
  • 举报
回复


kissnono(塔.拉夏的灵魂)

怎么改代码叱,说一下下可以不 (:
gxpang 2005-05-26
  • 打赏
  • 举报
回复

帮帮我啦,顶啦
kissnono 2005-05-26
  • 打赏
  • 举报
回复
hehe ,不会了。
我想到的笨方法。
记录执行的命令,如果是CHKDSK的话,把原有的“百分之”那行去掉,再加上新得到的“百分之”行。
gxpang 2005-05-25
  • 打赏
  • 举报
回复

不好意思哦, 写不进去啦,

总是显示 Service Unavailable


呵呵,问题是这样的 当 输入命令 "chkdsk" ,不像CMD里一样,而是会不断重复 “完成了百分之”

也就是说会有一百个 “完成了百分之 ” 出现,问题怎么解决呢
gxpang 2005-05-25
  • 打赏
  • 举报
回复

呵呵,谢谢 kissnono(塔.拉夏的灵魂)


呵,不过,还有一些小问题叱,我写在 Blog里了
加载更多回复(4)

16,555

社区成员

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

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