执行/打开执行c#中的程序

时间:2014-12-06 12:48:40

标签: c#

您好我创建了一种启动器应用程序,但是当我尝试使用启动按钮时它有一个问题,它只是没有做任何事情这里是我的代码。我需要的是一种通过您输入的程序选择Arma文件夹的方法,但是当我使用代码时它不起作用?

private void iniSetup()
    {
        //Directory Read
        tb_arma3dir.Text = ini.IniReadValue("Directory", "ARMA3");
        tb_addondir.Text = ini.IniReadValue("Directory", "ADDON");
    }

    private void arma2OACheck()
    {
        string arma3Path = tb_arma3dir.Text;

        if (arma3Path.EndsWith(@"\"))
        arma3Path = arma3Path.TrimEnd('\'');

        if(!File.Exists(arma3Path + "\\ArmA2OA.exe"))
        {
            a3dir_pic.Source = new BitmapImage(new Uri("./Resources/redX.png", UriKind.Relative));
            a2Good = false;
        }
        else
        {
            a3dir_pic.Source = new BitmapImage(new Uri("./Resources/greenCheck.png", UriKind.Relative));
            a2Good = true;
        }

    }


    private void addonDirCheck()
    {

        string addonPath = tb_addondir.Text;

        if (addonPath.EndsWith(@"\"))
            addonPath = addonPath.TrimEnd('\'');

        if (!Directory.Exists(addonPath))
        {
            addondir_pic.Source = new BitmapImage(new Uri("./Resources/redX.png", UriKind.Relative));
            addonGood = false;
        }
        else
        {
            addondir_pic.Source = new BitmapImage(new Uri("./Resources/greenCheck.png", UriKind.Relative));
            addonGood = true;
        }

    }
private void CompileLaunchParams()
    {
        string launchStr = "";


        //  Lauch parameters
        launchStr += @"-mod=@TCG" + @" -ip=31.186.251.207" + @" -port=2302" + " -nosplash" + "-world=empty" + "-nosplash";

        // Parse game path
        string filename = "iniSetup";

        // Start game process as administrator
        ProcessStartInfo info = new ProcessStartInfo(filename); // what do i use to select the program so it can execute?
        info.UseShellExecute = true;
        info.Verb = "runas";
        info.Arguments = launchStr;
        Process.Start(info);
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if(a2Good & addonGood ) // if the addon and arma is good it launches
        {
            CompileLaunchParams();
        }
        else
        {
            MessageBox.Show("Not all settings are configured, Please click the gear in the top right to define options!");
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试将所选路径保存到类中的字段,如:

private string Arma3Path;

private void arma2OACheck()
{
    string path = tb_arma3dir.Text;

    if (path.EndsWith(@"\"))
    Arma3Path = path.TrimEnd('\'') + "\\ArmA2OA.exe";

    if(!File.Exists(Arma3Path))
    {
        a3dir_pic.Source = new BitmapImage(new Uri("./Resources/redX.png", UriKind.Relative));
        a2Good = false;
    }
    else
    {
        a3dir_pic.Source = new BitmapImage(new Uri("./Resources/greenCheck.png", UriKind.Relative));
        a2Good = true;
    }

}

然后像

一样使用它
private void CompileLaunchParams()
{
    string launchStr = "";


    //  Lauch parameters
    launchStr += @"-mod=@TCG" + @" -ip=31.186.251.207" + @" -port=2302" + " -nosplash" + "-world=empty" + "-nosplash";

    // Parse game path
    string filename = Arma3Path;

    // Start game process as administrator
    ProcessStartInfo info = new ProcessStartInfo(filename); // what do i use to select the program so it can execute?
    info.UseShellExecute = true;
    info.Verb = "runas";
    info.Arguments = launchStr;
    Process.Start(info);
}