標籤:
解決啟動外部程式問題。
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace OuterExe 9 {10 class Program11 {12 static void InputFiles()13 {14 // In dir: Outer15 // Input main.cpp & data.in & data.out16 }17 18 static void Main(string[] args)19 {20 InputFiles();21 try22 {23 Process Run = Process.Start("Outer\\main.bat");24 if (Run != null)25 {26 Run.WaitForExit();27 if (Run.HasExited)28 Console.WriteLine("External program has exited.");29 }30 else31 {32 Run.Kill();33 Console.WriteLine("External program has exited by force.");34 }35 }36 catch(Exception exception)37 { Console.WriteLine(exception.Message); }38 39 string ErrorString = System.IO.File.ReadAllText("Outer\\error");40 string Answer = System.IO.File.ReadAllText("Outer\\data.out");41 string YourAnswer = System.IO.File.ReadAllText("Outer\\test.out");42 43 if (ErrorString != "")44 {45 Console.WriteLine("Compiling error:\n" + ErrorString);46 Console.ReadKey();47 return;48 }49 if (YourAnswer == Answer)50 Console.WriteLine("Accepted");51 else Console.WriteLine("Wrong Answer");52 Console.ReadKey();53 }54 }55 }
View Code
在 debug 裡面,有個 Outer 檔案夾。
用 main.bat 統籌程式的執行。
1 @echo off2 g++ Outer\main.cpp -o Outer\main 2> Outer\error3 Outer\main < Outer\data.in > Outer\test.out
View Code
要注意的是 g++ 的執行目錄不是 Outer,而是 debug,這是 bat 的特點決定的。
Visual Studio Form & DataBase (4)