2013-04-16 4 views
0

현재 내 게임의 런처를 프로그래밍하려고합니다. 제 문제는 다음과 같이 내 게임을 시작한 경우입니다.프로세스. 시작 문제 내 게임 시작

System.Diagnostics.Process.Start(@"F:\Freak Mind Games\Projects 2013\JustShoot\game.bat"); 

처리되지 않은 예외가 발생하거나 오류를 찾을 수 없습니다.

그러나 나는 이런 식으로 할 경우 : 그것은 작동

System.Diagnostics.Process.Start(@"game.bat"); 

합니다. 문제는 어디에 있습니까?

+2

후 정확한 예외 (안 의역) – banging

+2

가, BTW 상대 경로가 더 나은 솔루션으로 선택한 게임을 실행합니다. – banging

+0

은 \ bin 폴더에 Game.bat입니까? 첫번째 샘플에 대한 – Botonomous

답변

3

배치 파일이 프로그램 실행기와 같은 디렉토리에 있다고 가정합니다. 자동으로 위치를 결정

  • Application.ExecutablePath이 실행 파일의 경로입니다

    string executableDir = Path.GetDirectoryName(Application.ExecutablePath); 
    Process.Start(Path.Combine(executableDir, "game.bat")); 
    

    , 즉 F:\Freak Mind Games\Projects 2013\JustShoot\justshoot.exe.

  • Path.GetDirectoryName(...)은 디렉토리 부분 (예 : F:\Freak Mind Games\Projects 2013\JustShoot)을 가져옵니다.

  • Path.Combine(executableDir, "game.bat")

    game.bat으로도 염두에두고 실행 파일 경로가 "...\bin\Debug" 또는 "...\bin\Release"입니다 비주얼 스튜디오에서 시작할 때 즉 F:\Freak Mind Games\Projects 2013\JustShoot\game.bat


보관할를 디렉토리를 결합합니다. 따라서 배치 파일이 프로젝트 디렉토리에있는 경우 경로에서 이러한 부분을 제거 할 수 있습니다.

const string DebugDir = @"\bin\Debug"; 
const string ReleaseDir = @"\bin\Release"; 

string executableDir = Path.GetDirectoryName(Application.ExecutablePath); 
if (executableDir.EndsWith(DebugDir)) { 
    executableDir = 
     executableDir.Substring(0, executableDir.Length - DebugDir.Length); 
} else if (executableDir.EndsWith(ReleaseDir)) { 
    executableDir = 
     executableDir.Substring(0, executableDir.Length - ReleaseDir.Length); 
} 
Process.Start(Path.Combine(executableDir, "game.bat")); 

UPDATE

그것은 좋은 생각이 하드 코드 디렉토리가 아닙니다. 실행할 게임의 경로를 게임 실행기와 같은 디렉토리 (예 : "launch.txt")의 텍스트 파일에 배치하십시오. 각 줄에는 게임 이름과 함께 경로를 시작할 수있는 게임이 포함됩니다. 이처럼 :

 
Freak Mind Games = F:\Freak Mind Games\Projects 2013\JustShoot\game.bat 
Minecraft = C:\Programs\Minecraft\minecraft.exe 

는 형태 변수로 디렉토리를 정의합니다

string executableDir = Path.GetDirectoryName(Application.ExecutablePath); 
string launchFile = Path.Combine(executableDir, "launch.txt")); 

string[] lines = File.ReadAllLines(launchFile); 

// Fill the dictionary with the game name as key and the path as value. 
_games = lines 
    .Where(l => l.Contains("=")) 
    .Select(l => l.Split('=')) 
    .ToDictionary(s => s[0].Trim(), s => s[1].Trim()); 

는 그 다음에 게임 이름을 표시 :

private Dictionary<string,string> _games; 

지금과 같은이 게임의 목록을 ListBox :

listBox1.AddRange(
    _games.Keys 
     .OrderBy(k => k) 
     .ToArray() 
); 

마지막으로 올바르게 작업 디렉토리를 설정하면 어쨌든 .. 즉

string gamePath = _games[listBox1.SelectedItem]; 
var processStartInfo = new ProcessStartInfo(gamePath); 
processStartInfo.WorkingDirectory = Path.GetDirectoryName(gamePath); 
Process.Start(processStartInfo); 
+0

에 대한 빈/디버그 폴더에 game.bat 파일인가 확인이 내 코드입니다 : 네임 스페이스 GameLauncher { 공공 부분 클래스 형식 2 : 양식 { 공공 Form2를() { InitializeComponent(); } 개인 무효 Form2_Load (개체 보낸 사람, EventArgs 전자) { string executableDir = Path.GetDirectoryName (Application.ExecutablePath); Process.Start (Path.Combine (executableDir, @ "F : \ Freak 마인드 게임 \ 프로젝트 2013 \ JustShoot \ game.bat")); } } } 이 내가있어 두 개의 오류가 있습니다 : https://dl.dropboxusercontent.com/u/42689244/error1.JPG https://dl.dropboxusercontent.com/u/42689244 /error2.JPG –

+0

변수'executableDir'에는 이미 디렉토리가 있습니다. 따라서 경로의 디렉토리 부분을 삭제하십시오! 'Path.Combine (executableDir, "game.bat")'. 아마도'executableDir'은''F : \ Freak Mind Games \ Projects 2013 \ JustShoot "'를 포함하고있을 것입니다. –

+0

죄송합니다. 나는 이해하지 못한다. 어디에서 내 경로 F : \ Freak Mind Games \ Projects 2013 \ JustShoot \를 배치해야합니까? –