[ WPF ] How to execute a * .exe with a button in a main window, whose path is in a TextBox of a second window, please help me.

jack pelon 96 Reputation points
2020-08-18T20:57:51.9+00:00

Hi, I'm still a newbie. I have my project, in which I have 2 windows, a Main Window (Launcher.xaml) and another Second Window (Configuration.xaml). In the Main Window I have 2 buttons; a button that executes an * .exe (start game), whose path or address is in a TextBox in the second window. And the second Button calls the second window.

In the second window I have a TextBox and 2 Buttons, One is the one that calls the "OpenFileDialog" that when clicked, sends me a search dialog for the * .exe file (game.exe) and when I choose that program to execute, this write the address or path in the TextBox something like this; "D: \ folder \ game.exe". The following button saves the direction or route of the game to be able to execute it in the main window.
Well my question is how I can make the button of the main window (Start Game) execute the game whose address or path is in the TextBox of the second window, I would be very grateful if you could help me with it, please. Or some other alternative, I am already very grateful for it and its help.

I attach an image of my project:

https://i.pinimg.com/originals/fe/73/71/fe7371cc7ef94f05d91d606118974689.jpg

I also attach my project file:

https://drive.google.com/file/d/1Yknpp5eL2qMbyHaHOSR_Cmmfho4pK7aB/view?usp=sharing

Sorry for the inconvenience I really need your help. Thank you so much...

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,716 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,316 Reputation points
    2020-08-19T06:44:32.613+00:00

    Hi Jack,
    try demo in attached txt:

    18722-x.txt


3 additional answers

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2020-08-20T09:01:48.387+00:00

    I will show you a easy way not using ViewModel to get the date from the child window.
    1.Add the below code to your MainWindow.xaml.cs

     string ExeFileName;  
            public void GetExeFileName(string value) { ExeFileName = value; }  
            private void BtnEscogerJuego_Click(object sender, RoutedEventArgs e)  
            {  
                RutaDelJuego ConfigRuta = new RutaDelJuego();  
                ConfigRuta.sendMessage = GetExeFileName;  
                ConfigRuta.ShowDialog();  
            }  
    

    2.Add the below code in your WpfLanzarJuego.Xaml.cs:

     public delegate void SendMessage(string value);  
            public SendMessage sendMessage;  
            private void BtnGuardarSalir_Click(object sender, RoutedEventArgs e)  
            {  
                sendMessage(RutaTextBox.Text.ToString());  
                this.Close();  
            }  
    

    3.You can use the below code to host the exe in your MainWindow:
    19039-capture.png


  2. Peter Fleischer (former MVP) 19,316 Reputation points
    2020-08-21T10:42:27.24+00:00

    Hi Jack,
    without ViewModel you can use for DataContext in second window the CodeBehind of MainEindow:

    <Window x:Class="WpfApp1.MainWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp1"  
            mc:Ignorable="d"  
            Title="MainWindow" Height="450" Width="800">  
      <StackPanel>  
        <Button Content="Load Second Window" Click="Button_Click_1"/>  
        <Button Content="Execute exe" Click="Button_Click_2"/>  
      </StackPanel>  
    </Window>  
    

    CodeBehind:
    19494-x.png

    And second Window:

    19417-x.png

    0 comments No comments

  3. jack pelon 96 Reputation points
    2020-08-22T04:01:53.66+00:00

    Hello Miss DaisyTian-MSFT, and Mr. PeterFleischer-3316. I am very very grateful for your help, I am beginning to understand the "Binding, command and ViewModel". With a little self study I will understand that even more.

    Miss "DaisyTian" I like your code very much and I am doing tests and learning from it, I realize that I am just beginning to climb the mountain of programming in C # WPF, thank you very much ...

    I hope you have a good day and it will always be good for you. A warm hug from Peru. Thank you very much thank you friends ...