2017-03-08 158 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; 
相關問題