2015-12-24 98 views
0

我正在開發一個Windows應用程序,它將使用支持命令行的pdf2text試用軟件。在這個應用程序中,用戶需要指定pdf文件的位置。我可以打開cmd,但無法傳遞命令,或者某種程度上我的命令沒有執行。如何在vb.net中使用命令行

Imports System 
Imports System.IO 
Imports System.Diagnostics.Process 
Imports System.Diagnostics.ProcessStartInfo 

Public Class EDCS 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)  Handles Button1.Click 
    Dim dlgrslt As DialogResult = OpenFileDialog1.ShowDialog() 
    Dim fnames As String() = OpenFileDialog1.FileNames 
    Dim txtfnames As String 

    For i = 0 To fnames.Length - 1 
     If TextBox1.Text = "" Then 
      TextBox1.Text = fnames(i) 
     Else 
      TextBox1.Text = TextBox1.Text + "/" + fnames(i) 
     End If 
     txtfnames = fnames(i).Replace(".pdf", ".txt") 
     File.Create(txtfnames).Dispose() 

     Dim convertcommand As String = "textextract """ & fnames(i) & """  /to """ & txtfnames & """" 

     '/c will exit cmd and /k will keep it open 
     'Shell("cmd.exe /c textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt"") 

     'SendKeys.Send(convertcommand) 
     'SendKeys.Send("{ENTER}") 

     Dim p As New Process 
     p.StartInfo.FileName = "cmd.exe" 
     'p.StartInfo.WorkingDirectory = "C:\Program Files (x86)\Two Pilots\PDF2Text Pilot" 
     'p.StartInfo.Arguments = "textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt"" 
     p.StartInfo.UseShellExecute() = False 
     p.StartInfo.RedirectStandardInput = True 
     p.StartInfo.RedirectStandardOutput = True 
     p.Start() 
     p.StandardInput.WriteLine(convertcommand) 

     'Dim process As New Process() 
     'process.StartInfo.FileName = "cmd.exe " 
     'process.StartInfo.Verb = "runas" 
     'process.StartInfo.UseShellExecute = False 
     'process.StartInfo.RedirectStandardInput = True 
     'process.StartInfo.RedirectStandardOutput = True 
     'process.Start() 

     'process.StandardInput.WriteLine("textextract "C:\Users\user\Desktop\ Part 1.pdf" /to " C:\Users\user\Desktop\ Part 1.txt"") 
     'process.StandardInput.WriteLine("exit") 
     'process.Close() 

       Next 
End Sub 
End Class 

操作系統:Windows 7 vb.net開發

由於提前

+0

你傳遞你的命令行參數傳遞給Cmd.exe中,您需要將它們發送給該進程直通STND輸入,因此,如果你鍵入它的cmd.exe將執行它:'p.StandardInput.WriteLine( convertcommand)' –

+0

它給出了錯誤 system.argumentoutofrangeexception –

+0

你可以在這裏更新你的代碼嗎? –

回答

0

OK,在這裏你走,這應該工作,事實證明,pdf2text中有一個錯誤,但我設法通過在命令運行之前傳遞幾行空行來實現它的工作,然後在命令提示符關閉之前添加等待實用程序完成的工作。我猜你可能需要增加等待大文件。我在命令提示符中收到了一些「句柄無效」的消息,我懷疑它們也來自pdf2text,但它看起來像忽視它們的安全。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Dim dlgrslt As DialogResult = OpenFileDialog1.ShowDialog() 
     Dim fnames As String() = OpenFileDialog1.FileNames 
     Dim txtfnames As String 

     For i = 0 To fnames.Length - 1 
      If TextBox1.Text = "" Then 
       TextBox1.Text = fnames(i) 
      Else 
       TextBox1.Text = TextBox1.Text + "/" + fnames(i) 
      End If 
      txtfnames = fnames(i).Replace(".pdf", ".txt") 
      File.Create(txtfnames).Dispose() 

      Dim convertcommand As String = "textextract.exe """ & fnames(i) & """  /to """ & txtfnames & """" 

      Dim p As New Process 
      p.StartInfo.FileName = "cmd.exe" 
      p.StartInfo.Arguments = " /k" 
      p.StartInfo.UseShellExecute = False 
      p.StartInfo.RedirectStandardInput = True 
      p.StartInfo.RedirectStandardOutput = False 
      p.Start() 

      Using sw As StreamWriter = p.StandardInput 
       If sw.BaseStream.CanWrite Then 
        sw.WriteLine() 
        sw.WriteLine() 
        sw.WriteLine(convertcommand) 
        p.WaitForExit(3000) 
       End If 
      End Using 
     Next 
    End Sub 
+0

Sir SeanN,根據你的可能的解決方案,它給了我同樣的錯誤system.argumentoutofrangeexception –

+0

我試了多次,它每次都工作,我不能得到那個錯誤,什麼你有錯誤嗎?您是否正在使用我上面提到的或者您是否改變了它? – SeanN

+0

未處理的異常:System.ArgumentOutOfRangeException:該值必須很大 er大於或等於零並小於控制檯在該維度上的緩衝區大小 。 參數名稱:top 實際值爲-1。 在System.Console.SetCursorPosition(左的Int32,的Int32頂部) 在PDF2TextConverter.ConsoleMethods.ClearConsole(的Int32 NumberOfLines,我的Int32 nitialCursorTop) 在PDF2TextConverter.AppStart.Main(字串[] args) –