배치 파일이 프로그램 실행기와 같은 디렉토리에 있다고 가정합니다. 자동으로 위치를 결정
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);
후 정확한 예외 (안 의역) – banging
가, BTW 상대 경로가 더 나은 솔루션으로 선택한 게임을 실행합니다. – banging
은 \ bin 폴더에 Game.bat입니까? 첫번째 샘플에 대한 – Botonomous