如何在dataGridView中的选定驱动器上运行Cmd命令(chkdsk)

时间:2016-10-11 13:43:56

标签: visual-studio button datagridview cmd

我正在visual-studio中构建一个程序,使用cmd chkdsk命令检查和修复驱动器 我已经能够显示可用的驱动器列表是一个dataGridview 我想在任何选定的驱动器上运行chkdsk命令(由用户)请如何成功实现。此外,我希望chkdsk的过程和结果显示在文本框中。 问题是驱动器正在显示但是当选择一个并且我点击按钮修复时没有执行(即修复驱动器并在文本框中显示结果和过程)任何帮助/代码/建议将不胜感激,请注意这是注意控制台应用程序。我的代码已根据要求发布在此处。我添加了潜艇,我觉得问题可能来自。 ,但如果你想让我添加更多我的其他潜艇,我会。

 Public Class Form2
        Private binder As New BindingSource
        Private drives As New BindingList(Of DriveInfo)
        Private psi As ProcessStartInfo
        Private cmd As Process
        Private Delegate Sub invokeWithString(ByVal text As String)
        Dim BackgrounndWorker1 As New BackgroundWorker
    Private Shadows Sub OnClick(sender As Object, e As EventArgs) Handles btnfix.Click
            If TypeOf sender Is Button Then
                Dim btn = DirectCast(sender, Button)
                btn.Enabled = False
                If btn.Equals(btnfix) Then
                    Ddrives.Enabled = False
                    pgb.Visible = True
                    BackgroundWorker1.RunWorkerAsync(New List(Of DriveInfo)(From drive As DriveInfo In cldrive.CheckedItems Select drive))

                End If
            End If
        End Sub
Private Sub OnDoWOrk(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        If TypeOf sender Is BackgroundWorker Then
            Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
            If e.Argument IsNot Nothing AndAlso TypeOf e.Argument Is IEnumerable(Of DriveInfo) Then
                Dim drives As IEnumerable(Of DriveInfo) = DirectCast(e.Argument, IEnumerable(Of DriveInfo))
                For Each drive As DriveInfo In drives
                    Try
                        cmd.Kill()
                    Catch ex As Exception
                    End Try
                    TextBox1.Text = ""
                    psi = New ProcessStartInfo("Chkdsk /f")
                    Dim systemcoding As System.Text.Encoding =
                       System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
                    With psi
                        .UseShellExecute = False
                        .RedirectStandardError = True
                        .RedirectStandardOutput = True
                        .RedirectStandardInput = True
                        .CreateNoWindow = True
                        .StandardOutputEncoding = systemcoding
                        .StandardErrorEncoding = systemcoding
                    End With
                    cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True}
                    AddHandler cmd.ErrorDataReceived, AddressOf Async_data_received
                    AddHandler cmd.OutputDataReceived, AddressOf Async_data_received
                    Try
                        cmd.Start()
                        cmd.BeginOutputReadLine()
                        cmd.BeginErrorReadLine()
                    Catch ex As System.ComponentModel.Win32Exception
                    End Try
                Next
            End If
        End If
    End Sub

    Private Sub Async_data_received(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Me.Invoke(New invokeWithString(AddressOf Sync_output), e.Data)
    End Sub

0 个答案:

没有答案