0

Hello I want to create an update mechanic in my program (Windows Form Application). It makes it so when they press login, if there is an update; the program will first update then let them login.

if (!new WebClient().DownloadString(
            "ftp://username:password@wdasd.bplaced.net/test.txt").Contains("1.0.0.0")) {

} 
else 
{
    if (MessageBox.Show("New Update! Would you like to update?", "Yay!",
                            MessageBoxButtons.YesNo, MessageBoxIcon.Information) ==
                            System.Windows.Forms.DialogResult.Yes) 
    {
        Process.Start("ftp://username:password@wdasd.bplaced.net/wdasd.bplaced.net.zip");
    }
}

and the Button code

     private void button2_Click(object sender, EventArgs e)
            {


                //Hash
                var hash = SecurePasswordHasher.Hash("password");

                //Verify
                var result = SecurePasswordHasher.Verify("password", hash);

                if (


                    txtUsername.Text == "" || txt_Password.Text == "")
                {
                    MessageBox.Show("Please provide a Username and Password");
                    return;
                }
                try
                {
                    //Create SqlConnection
                    SqlConnection con = new SqlConnection(cs);
                    SqlCommand cmd = new SqlCommand("Select * from [break].[dbo].[tabl_login] where UserName=@username and Password=@password", con);
                    cmd.Parameters.AddWithValue("@username", txtUsername.Text);
                    cmd.Parameters.AddWithValue("@password", txt_Password.Text);
                    con.Open();
                    SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    adapt.Fill(ds);
                    con.Close();
                    int count = ds.Tables[0].Rows.Count;
                    //If count is equal to 1, than show frmMain form
                    if (count == 1)
                    {
                        MessageBox.Show("Login Successful!");

if (!new WebClient().DownloadString(
            "ftp://username:password@wdasd.bplaced.net/test.txt").Contains("1.0.0.0")) {

} 
else 
{
    if (MessageBox.Show("New Update! Would you like to update?", "Yay!",
                            MessageBoxButtons.YesNo, MessageBoxIcon.Information) ==
                            System.Windows.Forms.DialogResult.Yes) 
    {
        Process.Start("ftp://username:password@wdasd.bplaced.net/wdasd.bplaced.net.zip");
    }
}
                    }

                    else
                    {
                        MessageBox.Show("Login Failed!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

I am pretty sure Process.Start is not correct. What would I type into there in order for the program to look into the FTP server for an update?

Please no ClickOnce, I want it to load up update when they click login. Not have the installer.

RockyBoa
  • 89
  • 9
  • http://netsparkle.codeplex.com/ You are re (re-re-re-re) creating the wheel – Ňɏssa Pøngjǣrdenlarp Jan 06 '17 at 01:22
  • @Plutonix - didn't know about that project. Thanks for the link -- does it have a git repo ? – Kraang Prime Jan 06 '17 at 01:32
  • There are multiple options described in http://stackoverflow.com/q/5442259/62576, which definitely would make your question a duplicate. – Ken White Jan 06 '17 at 01:32
  • I added an example which you can use (with some tweaking to your taste) pretty much out of the box according to your code above. – Kraang Prime Jan 06 '17 at 01:32
  • @KraangPrime: It is not necessary for you to add a comment along with your answer; the user is automatically notified of new answers to the question. There's no need to comment to say *Look! Look! I posted an answer!* as well. – Ken White Jan 06 '17 at 01:33

2 Answers2

1

Perhaps something like the following would do the trick for you :

public async Task CheckForUpdates() {

    using(WebClient wc = new WebClient()) {
        string s = await wc.DownloadStringTaskAsync("ftp://username:password@wdasd.bplaced.net/test.txt");
        if(!s.Contains("1.0.0.0") {
            if (MessageBox.Show("New Update! Would you like to update?", "Yay!",
                MessageBoxButtons.YesNo, MessageBoxIcon.Information) ==
                System.Windows.Forms.DialogResult.Yes)
            {
               await wc.DownloadFileTaskAsync(
                "ftp://username:password@wdasd.bplaced.net/wdasd.bplaced.net.zip",
                @"c:\downloadlocation\tmpupdate.zip"
               );

               // do stuff with file downloaded
            }
        }
    }
    return;

}

You can call this method from inside another function (tagged with async in it's declaration) as follows :

await CheckForUpdates();

For example :

private async void button1_Click( object sender, EventArgs e ) {
    await CheckForUpdates();
}

async / await allow for non-blocking execution of background code while still allowing for UI interaction.


Using your revised button code :

private async void button2_Click(object sender, EventArgs e) {
 //Hash
 var hash = SecurePasswordHasher.Hash("password");

 //Verify
 var result = SecurePasswordHasher.Verify("password", hash);

 if (
  txtUsername.Text == "" || txt_Password.Text == "") {
  MessageBox.Show("Please provide a Username and Password");
  return;
 }

 try {
  //Create SqlConnection
  SqlConnection con = new SqlConnection(cs);
  SqlCommand cmd = new SqlCommand("Select * from [break].[dbo].[tabl_login] where UserName=@username and Password=@password", con);
  cmd.Parameters.AddWithValue("@username", txtUsername.Text);
  cmd.Parameters.AddWithValue("@password", txt_Password.Text);
  con.Open();
  SqlDataAdapter adapt = new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  adapt.Fill(ds);
  con.Close();
  int count = ds.Tables[0].Rows.Count;
  //If count is equal to 1, than show frmMain form
  if (count == 1) {
   MessageBox.Show("Login Successful!");
   await CheckForUpdates();
  } else {
   MessageBox.Show("Login Failed!");
  }
 } catch (Exception ex) {
  MessageBox.Show(ex.Message);
 }
}
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • I am a bit confused. If I wanted it to silently update from the FTP server, what should I say for // do stuff with file downloaded? – RockyBoa Jan 06 '17 at 01:42
  • the `//do stuff` is whatever you planned to do with the `.zip` file you downloaded. I am just assuming you aren't just going to download it, and that's it. Likely you will unzip it, move files around -- If not, then `//do stuff` means nothing. – Kraang Prime Jan 06 '17 at 01:45
  • the wait CheckForUpdates(); is giving me an error there. Since I want it to update from FTP when a button is placed. Would I place it at Form Load? – RockyBoa Jan 06 '17 at 01:49
  • @MonsterLegit101 - I updated the sample to be an `async` call on `button1`'s Click method. – Kraang Prime Jan 06 '17 at 01:51
  • I am confused. Button 1 is supposed to update and check for updates. How can I allow button_1 to allow the await "CheckForUpdates();" and the rest of the SQL code + login code? I will edit my original post for the button code. – RockyBoa Jan 06 '17 at 02:07
  • @MonsterLegit101 just add the `async` word to the button declaration. It's that simple. Put the `await CheckForUpdates()` wherever you planned to do that. It still runs in the same order, just it will not block (freeze) the UI while the files are being transferred. – Kraang Prime Jan 06 '17 at 02:10
  • see I added async and it completely breaks the Login process. It won't load the next Form. – RockyBoa Jan 06 '17 at 02:13
  • @MonsterLegit101 - It will load once the files have finished downloading. Again, this processes in sequence, but does not block the UI (eg, you can drag the window around while it is working.) -- assuming the credentials are valid, etc. Breakpoints are your friend. – Kraang Prime Jan 06 '17 at 02:17
  • Works great. Thanks for the code. Only question though. What would I do to make it update. You say c:\downloadlocation\tmpupdate.zip. What would go into that tmpupdate zip folder ? – RockyBoa Jan 06 '17 at 02:27
  • @MonsterLegit101 that is the location and filename you are saving what you download , as. You are grabbing `/wdasd.bplaced.net.zip` from your FTP server ... when you download, it needs to be stored somewhere -- this, is that file. You can call it what you want and do whatever you want with it (hence the `// do stuff` for things like extracting / running / moving / etc -- all stuff you need to write) – Kraang Prime Jan 06 '17 at 02:36
0

If I understand it correctly. This might be helpful

new WebClient().DownloadFile("ftp://username:password@wdasd.bplaced.net/wdasd.bplaced.net.zip",
                             "C:\\YourDownloadedFile.zip");
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • Anyway I could make the update load into the program without them being redirected to downloading a new program version? – RockyBoa Jan 06 '17 at 01:18
  • I am not sure what do you mean by that but you can download the file in background, Whereas for installation of the updated version has to be with users consent. – Mohit S Jan 06 '17 at 01:20
  • I have used programs where it does not require user consent. When the user presses "Login" and there is an update. The program immediately updates. – RockyBoa Jan 06 '17 at 01:24
  • so you can download in background and have a look at [run silent installer](http://stackoverflow.com/a/20670384/3796048) might help you – Mohit S Jan 06 '17 at 01:29
  • Thanks for the share, though I am confused about psi.FileName = desktopPath + "\\" + "MyInstaller_7.1.51.14.exe"; shouldn't this be an FTP server? – RockyBoa Jan 06 '17 at 01:39