Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

Using Visual Studio 2019 to attach a bat file to a Button

I have created a Form in Visual Studio 2019 and a button.
My goal is that when the button is pressed a .bat file is launched.

How can I add that command to the button/
Avatar of HainKurt
HainKurt
Flag of Canada image

something like this
private void button1_Click(object sender, EventArgs e)
        {
            Process proc = null;
            try
            {
                string batDir = string.Format(@"D:\");
                proc = new Process();
                proc.StartInfo.WorkingDirectory = batDir;
                proc.StartInfo.FileName = "testbat.bat";
                proc.StartInfo.CreateNoWindow = false;
                proc.Start();
                proc.WaitForExit();
                MessageBox.Show("Bat file executed !!");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
        }

Open in new window

Avatar of E=mc2

ASKER

I am in Visual Studio
what are the steps to attach the bat file?
what are the steps to attach the bat file? 

I dont get what thıs means...
the above code is to run a bat file on a button click...

you just modify the path...
Avatar of David Johnson, CD
add the batch file to your solution directory,
Add it to the solution by add existing file
Right click the batch file and select copy always

using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text = "Press Button to Launch Batch";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process proc = null;
            try
            {
                string batDir = string.Format(@".\");
                proc = new System.Diagnostics.Process();
                proc.StartInfo.WorkingDirectory = batDir;
                proc.StartInfo.FileName = "hello World.cmd";
                proc.StartInfo.CreateNoWindow = false;
                proc.Start();
                proc.WaitForExit();
                label1.Text="Bat file executed !!";
            }
            catch (Exception ex)
            {
               label1.Text=ex.StackTrace.ToString();
            }
        }
    }

Open in new window

Avatar of E=mc2

ASKER

Thanks David, where is the solution directory?
Proof of Concept
right click on a blank spot in the solution explorer and then look near the bottom.. open folder in file explorer or open folder in terminal 
Avatar of E=mc2

ASKER

Thanks David, let me give this a try, many thanks for the video this really helps.. 
Avatar of E=mc2

ASKER

Thanks to both of you, it did not work, I give up on this... 
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of E=mc2

ASKER

I will try to access your drive.. thanks.. 
it did not work, I give up on this... 

what is not working?
what is your current code
and what is the error message...
Avatar of E=mc2

ASKER

David, I received the project.. how do I import it into my Vistual Studio 2019?
unzip it , open a project, browse to the folder containing the .sln file and open the .sln file

Avatar of E=mc2

ASKER

Perfect, I have it open and it works, however I can't see the pane where it shows the button and then I can view the code..  
Is there a way I can see it for example when I am working on the button itself?
Your form shows you the form (as it will be shown. right click and show code to see the c# code.
double click on the batch file to edit the batch file.