[C#] How to Update Your Programs

Discussion in 'Programming & Scripting' started by Zer0-One, Feb 5, 2010 with 23 replies and 908 views.

  1. Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    How to Update Your Programs


    A lot of people want to know how to implement an update system, so here ya go.

    Here's how it lays out: You have an XML file hosted on a website. This file has info in it like the current version of the program or some other significant piece of info. When a user of your application clicks the update button, you want to check that XML file to see whether the version number in the file is greater than the version number of their program (the version number of the program can be saved as a setting, or stored as a constant, although i prefer the former). If it is, the application will automatically download and install the newer version.

    So I'll walk you through the necessary code:

    Preparation:


    Add the System.XML, System.Net, and System.Diagnostics namespaces to your app.

    Checking What the Latest Version Is:

    Code:
    //Set the XmlTextReader instance to work with your xml file
    
    XmlTextReader XMLR = new XMLTextReader("http://www.website.com/version.xml");
    
    //Move position in the file to the part with your version number.
    
    XMLR.MoveToContent();
    
    //Read the version number, and convert it to an int.
    
    int version = Convert.ToInt16(XMLR.ReadString());
    
    That was simple. Now,

    Comparing Version Numbers/Downloading and Installing


    Code:
    [LEFT]//Compare the latest version with your version.
    
    if (version > Properties.Settings.Default.Version)
    {
    //Create an instance of WebClient, which can handle downloading web data. 
    
    WebClient Client = new WebClient();
    
    //Download the file, and save it as "Setup.exe"
    
    Client.DownloadFile("http://www.website.com/Setup.exe", "Setup.exe");
    
    //Inform the user that you need to close the program to perform updates.
    
    MessageBox.Show("Program Must Now Close To Perform Updates", "Update");
    
    //Start your updated setup file
    
    Process.Start("Setup.exe");
    
    //Close the program so that you can overwrite files.
    
    this.Close();
    }
    [/LEFT]
    
    And there you go. That's really all there is to it. It's a lot simpler than you may think thanks to all the libraries already made for us. It's a good idea to wrap this in a Try block and then if there's an error, use a catch block to inform the user of the error.

    Post if you have any questions, and if you have improvements, PM me and i will credit you with them.
     
    • Like Like x 1
  2. Thilo

    Thilo Contributor

    Messages:
    2,786
    Ratings:
    1,200
    Re: How to Update Your Programs

    That really isnt XML, just sr.ReadLine();

    Might as well make a .txt file on a hosting site; much easier to set up.

    I would call this using XML since you dont even really use the tags, which is waht XML is all about.
     
  3. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    Re: How to Update Your Programs


    point taken, but if you want to check for more than one thing, you need to do it this way. Plus, this way leaves room for improvement. You won't have to retype much should you decide to start checking for more than one thing.
     
  4. DrXthirst

    DrXthirst Join Date: March 2006

    Messages:
    1,122
    Ratings:
    326
    Re: How to Update Your Programs

    Code used in "GamerTag Follower."
     
    • Like Like x 1
  5. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    Re: How to Update Your Programs

    0.0

    there's one way to do it i guess.
     
  6. GoldBl4d3

    GoldBl4d3 Retired Retired

    Messages:
    3,758
    Ratings:
    1,328
    Re: How to Update Your Programs

    Yeah, but updaters like that are gay. You want to go full out when you make an updater. Makes it a better overall app.
     
  7. Haxalot88

    Haxalot88 VIP VIP

    Messages:
    8,051
    Ratings:
    2,482
    Re: How to Update Your Programs

    If you want to go full out on an updater, you put your main functionality in seperate application libraries all organized and ****, and you simply update those each time your app starts. One system I recently worked on actually used a database for updates, and returned specially formatted data.
     
  8. rawrzorz

    rawrzorz Banned

    Messages:
    58
    Ratings:
    12
    Re: How to Update Your Programs

    Making an XML file is a waste of space and more bandwidth than needed, just use a txt file, if you want to check for more than one thing, then just make another line, say you want to start with just the version, well, in 7 - 15 bytes, you can contain your version:
    "x.x.x.x"
    And then just ReadLine() and close the stream, this way the user isn't reading extra XML data that isn't need for just checking a version string

    Now let's say later on you decide to read a download link, well just add the download link on the next line and add another ReadLine() and vwallah, you have the version and the download link.

    Just keep adding lines for what is needed later on instead of using an XML file because its a waste of space, you have to waste bytes for the chars that make up the structure of the XML file when it's unneeded so your way works but is not efficient
     
    • Like Like x 1
  9. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    Re: How to Update Your Programs

    well here's the thing, instead of keeping a silly map of all the things on all the different lines, i enjoy nice neat XML tags.
     
  10. sgt frankieboy

    sgt frankieboy Enthusiast

    Messages:
    724
    Ratings:
    133
    Re: How to Update Your Programs

    DMK06 Looks nice But it's in C#:frown:
    @DrXthirst
    Post your Code like this Don't Use Quote's
    [noparse]
    Code:
    
    
    [/noparse]
    it will look like this
    Code:
    [CODE HERE]
     
  11. DrXthirst

    DrXthirst Join Date: March 2006

    Messages:
    1,122
    Ratings:
    326
    Re: How to Update Your Programs

    Yeah, so that it can stretch the screen? No thanks. :smile:
    I've tried that with much of my code, but a single line may be too long for Se7enSins' thin forum skin.

    EDIT: I've been here for 4 years. I know how to use forums. <3
     
    • Like Like x 1
  12. Thilo

    Thilo Contributor

    Messages:
    2,786
    Ratings:
    1,200
    Re: How to Update Your Programs

    At DJ's method: Winsauce.

    That is what my apps have been using; neat, clean, but I have to switch over to XML if I expect to get better at coding.
     
    • Like Like x 1
  13. DrXthirst

    DrXthirst Join Date: March 2006

    Messages:
    1,122
    Ratings:
    326
    Re: How to Update Your Programs

    XML is a good way to do it... it's actually very fast as well... as seen in "GamerTag Follower." :smile:
     
  14. Sol Invictus

    Sol Invictus Contributor

    Messages:
    2,658
    Ratings:
    552
    Re: How to Update Your Programs

    No need to quote "GamerTag Follower" in every post!!!!!!!!
     
    • Like Like x 4
  15. DrXthirst

    DrXthirst Join Date: March 2006

    Messages:
    1,122
    Ratings:
    326
    Re: How to Update Your Programs

    And why's that?
     
  16. OP
    Zer0-One

    Zer0-One Experienced Member

    Messages:
    2,233
    Ratings:
    245
    Re: How to Update Your Programs

    0.0

    for the record, i'm never going to post a tutorial again until i'm a ****ing guru about the topic im posting on.
     
  17. Sol Invictus

    Sol Invictus Contributor

    Messages:
    2,658
    Ratings:
    552
    Re: How to Update Your Programs

    We all know that you made a Gtag viewer, you dont have to clearly state in every post that "i made teh Gtag viewer".
     
  18. DrXthirst

    DrXthirst Join Date: March 2006

    Messages:
    1,122
    Ratings:
    326
    Re: How to Update Your Programs

    When I said, "as seen in GamerTag Follower," I was referring to how quickly my update code checks to see if you have a new version. If people wanted to see how quickly it updates, then they can download it and see... I even posted the source code, so if they liked it, then they could use mine. Thanks.
     
  19. Sol Invictus

    Sol Invictus Contributor

    Messages:
    2,658
    Ratings:
    552
    Re: How to Update Your Programs


    Sorry about that, I must have misunderstanded!:cursing:
     
  20. DrXthirst

    DrXthirst Join Date: March 2006

    Messages:
    1,122
    Ratings:
    326
    Re: How to Update Your Programs

    Misunderstood.
    And it's okay.
     

Share This Page