E=mc2
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/
My goal is that when the button is pressed a .bat file is launched.
How can I add that command to the button/
ASKER
I am in Visual Studio
what are the steps to attach the bat file?
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...
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
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();
}
}
}
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
ASKER
Thanks David, let me give this a try, many thanks for the video this really helps..
ASKER
Thanks to both of you, it did not work, I give up on this...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...
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
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?
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.
double click on the batch file to edit the batch file.
Open in new window