63 215
Assignments Done
99,3%
Successfully Done
In July 2018

Answer to Question #21197 in C# for saurabh mishra

Question #21197
I want to write a C# program which will launch an .exe (this is an easy job, I know how to do it.) but now I want to add some more functionality to it. I want to take user inputs to my program and want to pass them to running .exe as a input.Also I want to click on a particular button available on the exe window. Important thing is, I want to do it programatically without user knowledge that we are actually running that exe in background.
Expert's answer
using System;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

class MainForm : Form
{
Button execute;
TextBox targetProcess;

public void ExecuteClick(object sender, EventArgs e)
{
string processToKill = targetProcess.Text;
if (!processToKill.EndsWith(".exe"))
processToKill += ".exe";

ProcessStartInfo startInfo = new ProcessStartInfo("taskkill", "/F /IM " + processToKill);
startInfo.WindowStyle = ProcessWindowStyle.Hidden; //hide process window
Process.Start(startInfo);
}

public MainForm() : base()
{
this.SuspendLayout();
this.Size = new Size(150, 106);
this.MaximizeBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.StartPosition = FormStartPosition.CenterScreen;

execute = new Button();
execute.Location = new Point(12, 36);
execute.Size = new Size(120, 32);

execute.Text = "Kill process";
execute.Click += new EventHandler(ExecuteClick);

targetProcess = new TextBox();
targetProcess.Location = new Point(12, 12);
targetProcess.Size = new Size(120, 32);

this.Controls.Add(targetProcess);
this.Controls.Add(execute);
this.ResumeLayout();
}
}

class Program
{
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions