c#在winforms中启动控制台应用程序

问题描述 投票:0回答:1

我正在尝试为我的游戏制作启动器,我喜欢在后台添加音乐播放器,但如果我开始这个过程,它会立即失败。

private void btnStartMusic_Click(object sender, EventArgs e)
{
    ProcessStartInfo proc = new 
    ProcessStartInfo("MusicPlayer\\MemequickieMusicPlayer.exe");
    proc.CreateNoWindow = true;
    proc.WindowStyle = ProcessWindowStyle.Hidden;
    proc.RedirectStandardError = false;
    proc.RedirectStandardInput = false;
    proc.RedirectStandardOutput = false;
    proc.UseShellExecute = false;
    Process.Start(proc);
}

任何帮助表示赞赏。

c# winforms
1个回答
2
投票

尝试使用exe的完整路径并设置工作目录;假设exe在您的可执行文件夹中:

string path = Path.Combine(Application.StartupPath, "MusicPlayer");

ProcessStartInfo proc = new 
    ProcessStartInfo(Path.Combine(path, "MemequickieMusicPlayer.exe")); 

proc.WorkingDirectory = path;

如果错误仍然存​​在并且您要调试输出,请更改:

proc.RedirectStandardOutput = true;

创建这样的过程:

Process process = new Process(proc);
process.Start();

while (!process.StandardOutput.EndOfStream) {
    System.Diagnostics.Debug.Write(process.StandardOutput.ReadLine());
}

您现在应该在输出窗口中看到输出。

© www.soinside.com 2019 - 2024. All rights reserved.