-3
private void button1_Click(object sender, EventArgs e)
{
    // here i want to replace the files with the same name from a download link //
    Application.Exit();
}

I am fairly new to C# and this might not make sense. When the button is clicked, it should replace the files in a folder and replace them with the files from a download link or other source.

Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
BLUE FADE
  • 3
  • 1

1 Answers1

0

Use this code:

WebClient myClient = new WebClient();
myClient.DownloadFile(yourLink, YourFileLocation);

This should overwrite the file with the new one.

----EDIT #1

WebClient myClient = new WebClient();
myClient.DownloadFile("https://website.com/test.exe", @"C:\test.exe");

This should replace the test.exe on your computer with the test.exe from the website.

----EDIT #2

To replace the file on someone else's computer, you would make sure that when the file is downloaded, it is always in the same place on every computer. Here is an example of putting it in "My Documents"

WebClient myClient = new WebClient();
myClient.DownloadFile("https://website.com/test.exe", @"C:\Users\" + Environment.UserName + @"\Documents\test.exe");
Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • For my file location, do I put where the file is for them? – BLUE FADE Nov 23 '19 at 17:21
  • The file location is just where the file you download will be saved. Put in the location of the old file, and it will overwrite it (as long as it has the same name) –  Nov 23 '19 at 17:22
  • I will have other people using the program. Can you add an example of the code if it's going to replace test.exe? – BLUE FADE Nov 23 '19 at 17:26
  • What exactly are you hoping to accomplish? Are you making some kind of auto-updater? –  Nov 23 '19 at 17:27
  • Yes, that is what I'm trying to do. – BLUE FADE Nov 23 '19 at 17:28
  • What if I want to replace it on someone else's computer? – BLUE FADE Nov 23 '19 at 17:34
  • @Duel I have edited your answer because in windows the directory character is `\\` – Cleptus Nov 23 '19 at 17:50
  • Can I use application.startup path to replace files there? – BLUE FADE Nov 23 '19 at 18:06
  • Would this work: WebClient myClient = new WebClient(); myClient.DownloadFile("https://website.com/test.exe", Application.StartupPath); or would i have to replace myclient with webclient? – BLUE FADE Nov 23 '19 at 18:26
  • That would not work. You cannot just have the path. You would need an end to the path. WRONG: "Application.StartupPath" –  Nov 23 '19 at 20:48
  • RIGHT: Application.StartupPath + "/test.exe" –  Nov 23 '19 at 20:49