如何在vb.net中使用命令行

如何在vb.net中使用命令行

问题描述:

我正在开发一个Windows应用程序,它将使用支持命令行的pdf2text pilot软件。在此应用程序中,用户需要指定pdf文件的位置。我能够打开cmd但无法将命令传递给它或者某种方式我的命令没有被执行。



I m developing a windows application that will use pdf2text pilot software which supports command line. In this application user needs to specify location of the pdf file. I m able to open cmd but could not pass commands to it or somehow my commands are not getting executed.

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()
            

            '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开发者



先谢谢



OS: Windows 7
vb.net developer

Thanks in Advance

你最糟糕的事情是使用CMD.EXE。我不知道为什么这么多人犯了这个错误,假设这是用户思考(非工程)的转移。你绝对不需要它。

说,你有textextract.exe。从 p.StartInfo.Arguments 中删除​​texteatract。确保参数正确。确保textextract.exe位于正确的路径中。而不是与CMD.EXE一致写道

The worst thing you are doing is using CMD.EXE. I don't know why so many people makes this mistake, suppose this is a diversion of "user thinking" (non-engineering). You absolutely don't need it.
Say, you have "textextract.exe". Remove "texteatract" from p.StartInfo.Arguments. Make sure the arguments are correct. Make sure "textextract.exe" is in the right path. Instead of line with "CMD.EXE" write
p.StartInfo.FileName = "textextract.exe"





这就是全部。



很奇怪您希望您的应用程序能够与您一起工作参数值。应该很明显这是错误的。实际上,有一种方法可以将CMD.EXE与应用程序一起使用,但我甚至不讨论它。这样做会非常疯狂。你的问题已经解决了。



-SA



That's all.

It's so strange that you expected that your application should work with you Arguments value. It should be pretty obvious that it's wrong. Actually, there is a way to use CMD.EXE with the application, but I don't even discuss it. Doing it would be totally insane. Your problem is already solved.

—SA