【问题标题】:command prompt commands executing in windows application在 Windows 应用程序中执行的命令提示符命令
【发布时间】:2017-03-08 04:10:07
【问题描述】:

我正在尝试在 Windows 应用程序中运行另一个应用程序,即 tetpdflib。 tetpdflib 仅在命令提示符下运行。当我将 exe 拖放到命令提示符时,它将执行。为此我遵循了一些编码

            Process tetmlProcess = new Process();
            tetmlProcess.StartInfo.CreateNoWindow = true;
            tetmlProcess.StartInfo.RedirectStandardOutput = true;
            tetmlProcess.StartInfo.UseShellExecute = false;
            tetmlProcess.StartInfo.FileName = @"cmd.exe";
            tetmlProcess.StartInfo.Arguments = "cd C:\\Users\\sw_chn\\Documents\\PDFlib\\TET 5.0 32-bit\\bin\\tet.exe";

            tetmlProcess.Start();

但我无法获得输出......而且我还需要运行以下命令提示符行

cd tet.exe 和 tet -m 文件名

如何在该进程中执行这些命令。

这就是完整的编码

    public static string inputfile = string.Empty;
    public static string outputfolder = string.Empty;

    private void btninputbrowse_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog inputFileBrowser = new OpenFileDialog();
        DialogResult result = inputFileBrowser.ShowDialog();
        if (result == System.Windows.Forms.DialogResult.OK)
        {
            inputfile = inputFileBrowser.FileName;
            txtinput.Text = inputFileBrowser.FileName;
        }
    }

    private void btnoutputbrowse_Click(object sender, RoutedEventArgs e)
    {
        FolderBrowserDialog folderbrowsing = new FolderBrowserDialog();
        DialogResult result = folderbrowsing.ShowDialog();
        if (result == System.Windows.Forms.DialogResult.OK)
        {
            outputfolder = folderbrowsing.SelectedPath;
            txtoutput.Text = folderbrowsing.SelectedPath;
        }
    }

    private void btnok_Click(object sender, RoutedEventArgs e)
    {
        MoveInputFileToOutPutFolder();
    }

    private void MoveInputFileToOutPutFolder()
    {
        try
        {
            string[] splitinput = inputfile.Split('\\');
            outputfolder = System.IO.Path.Combine(outputfolder,splitinput.LastOrDefault());
            if (File.Exists(outputfolder))
            {
                File.Delete(outputfolder);
            }
            File.Copy(inputfile,outputfolder);
            TetmlApplicationRunning();
        }
        catch (Exception)
        {

            throw;
        }
    }

    private void TetmlApplicationRunning()
    {
        try
        {
            Process tetmlProcess = new Process();
            //tetmlProcess.StartInfo.CreateNoWindow = true;
            //tetmlProcess.StartInfo.RedirectStandardOutput = true;
            //tetmlProcess.StartInfo.UseShellExecute = false;
            tetmlProcess.StartInfo.FileName = @"C:\\Users\\sw_chn\\Documents\\PDFlib\\TET 5.0 32-bit\\bin\\tet.exe";
            tetmlProcess.StartInfo.WorkingDirectory = @"C:\\Users\\sw_chn\\Documents\\PDFlib\\TET 5.0 32-bit\\bin";
            tetmlProcess.StartInfo.Arguments = "tetml -m wordplus" + inputfile;
            tetmlProcess.Start();
        }
        catch (Exception)
        {

            throw;
        }
    }
}

}

【问题讨论】:

  • 可能需要设置WorkingDirectory
  • 是的,如何执行这两个 cmets
  • 应该是"tetml -m wordplus " + inputfile; Path.GetFileName
  • 您也可能需要指定输出-o 选项。从来没有用过tet,只是查了一下手册
  • 我没有得到最终产品 tetmlProcess.StartInfo.RedirectStandardError = true; tetmlProcess.StartInfo.WorkingDirectory = @"C:\Users\sw_chn\Documents\PDFlib\TET 5.0 32-bit\bin"; tetmlProcess.StartInfo.FileName = '"'+"C:\\Users\\sw_chn\\Documents\\PDFlib\\TET 5.0 32-bit\\bin\\tet.exe"+'"'; tetmlProcess.StartInfo.Arguments = "tet -m wordplus "+ System.IO.Path.GetFileName(outputfolder);

标签: c# process windows-applications


【解决方案1】:

你可以像下面那样做。你不需要运行cmd.exe,你可以直接运行tet.ext。在代码中添加了 cmets。

Process tetmlProcess = new Process();
tetmlProcess.StartInfo.CreateNoWindow = true;
tetmlProcess.StartInfo.RedirectStandardOutput = true;
tetmlProcess.StartInfo.UseShellExecute = false;
// Instead of cmd.exe you run the tet.exe
tetmlProcess.StartInfo.FileName = @"C:\\Users\\sw_chn\\Documents\\PDFlib\\TET 5.0 32-bit\\bin\\tet.exe";
//Set The working directory to C:\Users\sw_chn\Documents\PDFlib\TET 5.0 32-bit\bin\ if needed
tetmlProcess.StartInfo.WorkingDirectory = @"C:\\Users\\sw_chn\\Documents\\PDFlib\\TET 5.0 32-bit\\bin";
//Use the arguments required for tet.exe
tetmlProcess.StartInfo.Arguments = "-m filename";

tetmlProcess.Start();

注意:此代码是在这里直接输入的(现在无法访问 Visual Studio),因此可能包含语法错误。仅将其视为指南。

【讨论】:

  • 但是当我运行该工具时我没有得到准确的输出,我得到了带有文本覆盖的 .tetml 文件。但是当我运行时,我只得到没有任何文本的 txt 文件,我将发布完整的编码
  • 进程 tetmlProcess = new Process(); tetmlProcess.StartInfo.WorkingDirectory = @"C:\Users\sw_chn\Documents\PDFlib\TET 5.0 32-bit\bin"; tetmlProcess.StartInfo.FileName = '"'+"tet.exe"+'"'; tetmlProcess.StartInfo.Arguments = @"tet -m wordplus D:\DailyWork\March\JOURNAL-ISSUE_6_3924-3930.pdf"; tetmlProcess.Start();那是什么错误?
  • 你应该使用.FileName = "tet.exe"并且你需要tet作为参数吗?
【解决方案2】:

试试下面的 sn-p:

        var proc = new ProcessStartInfo();
        string yourCommand;
        yourCommand = "calc.exe";
        proc.UseShellExecute = true;
        proc.WorkingDirectory = @"C:\Windows\System32";
        proc.FileName = @"C:\Windows\System32\cmd.exe";
        proc.Arguments = "/c " + yourCommand;
        proc.WindowStyle = ProcessWindowStyle.Normal;
        Process.Start(proc);

我运行计算器;您可以将程序作为 tet.exe 运行,并且您必须根据您的 .exe 文件设置其他参数,例如 WorkingDirectory 和 FileName。 这行代码 proc.WindowStyle = ProcessWindowStyle.Normal; 显示 cmd.exe 窗口。如果你不打算显示,把上面提到的代码行改成proc.WindowStyle = ProcessWindowStyle.Hidden; 我希望能成功!

【讨论】:

    【解决方案3】:

    我不会尝试模拟命令提示符,而是直接执行应用程序。我假设这个应用程序的输出被发送到控制台。您可以重定向此输出,但无法将其与 shell 执行结合使用。我使用应用程序的完整路径名,我在“选项”类中设置并存储在注册表中。一个例子:

        static public String CompileScript(String InputFile, String OutputFile)
            {
            Process Compiler = new Process();
            String Result = String.Empty;
            try
                {
                Compiler.StartInfo.FileName = CLuaCreatorOptions.TrainSimulatorDirectory + "\\luac.exe";
                Compiler.StartInfo.Arguments = "-v -o " + OutputFile + " " + InputFile;
                Compiler.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                Compiler.StartInfo.CreateNoWindow = true;
                Compiler.StartInfo.UseShellExecute = false;
                Compiler.StartInfo.RedirectStandardOutput = true;
                Compiler.StartInfo.RedirectStandardError = true;
                Compiler.Start();
                Result = Compiler.StandardOutput.ReadToEnd() + "\n" + Compiler.StandardError.ReadToEnd();
                Compiler.WaitForExit();
                }
            catch (Exception e)
                {
                return "Error compiling script " + e.Message + "\r\n" + Result;
                }
            return Result;
            }
    

    此示例运行 LUA 编译器并将所有错误消息(异步)返回到字符串“Result”。我在调用此编译器的表单应用程序中的多行文本框中显示此刺痛。

    这三行可以帮助您重定向输出。

            Compiler.StartInfo.UseShellExecute = false;
            Compiler.StartInfo.RedirectStandardOutput = true;
            Compiler.StartInfo.RedirectStandardError = true;
    

    要实际获取信息,您需要运行应用程序并获取输出。您需要等到应用程序退出才能获得完整的结果,最后三行为您完成:

            Compiler.Start();
            Result = Compiler.StandardOutput.ReadToEnd() + "\n" + Compiler.StandardError.ReadToEnd();
            Compiler.WaitForExit();
    

    最后,您可能希望禁止显示命令窗口。此代码将为您执行此操作:

            Compiler.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            Compiler.StartInfo.CreateNoWindow = true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多