So I recently wanted to try out C# and make an app that executes a file on a button click without the file being installed on the machine so the button downloads it silently and executes it once downloaded.
So I tried the following code hoping that it will execute the .exe but it only downloaded it without running it. Here's the full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;
using System.IO;
namespace InvizCheatz_Loader
{
public partial class InvizCheatz : Form
{
public InvizCheatz()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://invizcheatz.com/Downloads/CF-Multi_Tool.exe");
}
}
}
The above code downloaded the file although I wanted to do it silently open it automaticaly.
CodePudding user response:
you have to download the file before you can start it!
private void button1_Click(object sender, EventArgs e)
{
string LinkToFile = @"https://invizcheatz.com/Downloads/CF-Multi_Tool.exe";
string FileName = "CF-Multi_Tool.exe";
string filePath = DownloadFile(LinkToFile, FileName);
StartFile(filePath);
}
string DownloadFile(string link, string fileName)
{
WebClient wc = new WebClient();
//download the file and renamed it
wc.DownloadFile(link, fileName);
//return the path where the file is
return Environment.CurrentDirectory "\\" fileName;
}
void StartFile(string filePath)
{
//start the process
System.Diagnostics.Process.Start(filePath);
}