2017-03-08 159 views
0

我在Windows应用程序中尝试运行另一个应用程序,它是tetpdflib。该tetpdflib只在命令提示符下运行。当我拖放EXE到命令提示符时,它会执行。对于我跟着一些编码在windows应用程序中执行的命令提示符命令

  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; 
     } 
    } 
} 

}

+0

可能是您需要设置[WorkingDirectory](https://msdn.microsoft.com/en-us/library/system。diagnostics.processstartinfo.workingdirectory(v = vs.110).aspx) – bansi

+0

yes如何执行这2个注释 –

+0

应该是''tetml -m wordplus“+ inputfile;'< - 注意空格。另外请注意,您不需要拆分字符串并获取最后一部分以获取可使用[Path.GetFileName](https://msdn.microsoft.com/en-us/library/system.io.path。 getfilename(v = vs.110).aspx) – bansi

回答

0

你可以像下面。你不需要运行cmd.exe就可以直接运行tet.ext。在代码中添加注释。

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),因此可能包含语法错误。只将这视为指导。

+0

但我没有得到确切的输出,当我运行该工具时,我得到的.tetml文件与文本coversion.but当我运行通过vs只是我只有txt文件与任何文本我将发布完整编码 –

+0

Process tetmlProcess = new Process (); tetmlProcess.StartInfo.WorkingDirectory = @“C:\ Users \ sw_chn \ Documents \ PDFlib \ TET 5.0 32位\ bin”; tetmlProcess.StartInfo.FileName ='“'+”tet.exe“+'”'; tetmlProcess.StartInfo.Arguments = @“tet -m wordplus D:\ DailyWork \ March \ JOURNAL-ISSUE_6_3924-3930.pdf”; tetmlProcess.Start(); 那是什么错误? –

+0

你应该使用'.FileName =“tet.exe”'并且你需要tet作为参数吗? – bansi

0

尝试下面的代码片段:

 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; 我希望能解决这个问题!

0

我owuld不尝试模拟命令提示符,但直接执行应用程序。我假设这个应用程序的输出被发送到控制台。您可以重定向这个输出,但不可能将其与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并返回所有错误消息(异步)为字符串“结果”。我在窗体应用程序调用这个编译器的多行文本框中显示了这个刺。

这三行代码让你重定向输出。

 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; 
相关问题