.NET Coldfire202's C# Updater GitHub Thread

Discussion in 'Source Code & Tutorial Database' started by MatthewH, Apr 15, 2015 with 28 replies and 1,799 views.

  1. MatthewH

    MatthewH Member

    Messages:
    1,612
    Ratings:
    613
    After seeing quite a few responses to these other threads with updaters that are honestly poorly made (who would honestly use PasteBin to get information from). None the less, no offence to any of you for that...it's just..it's not the best idea in the interest of the user.

    But I digress.
    I made an updater a few months ago for some reason and I'm trying to remember it right now but yeah, I can't.
    So I decided to release it.

    However, I'm not just releasing you a zip file. I decided to create a repository for it and I'll do my best to keep it updated and add new things.
    Feel free to add on if you like. Just make a pull request.

    [Click here to view the link]

    I've kept the code documented as much as possible (I'm not very good at documenting it)

    Using the project
    It was made in Visual Studio 2012, so you may need that or higher? If you don't, great!
    Other than that...just upload the Json file to your server (make sure to edit the links and such inside) and then edit the program (main.cs) to your liking also.

    Included in the project
    Well as of right now, there's two things included in the repo. The first of which is an example application that pulls a build and version from an online Json file and tell you if there's an update or not, this is included for people who don't know how to include the updater in their project.
    Next is a single executable updater. If your project only has one executable and only one executable or if it's an automatic zip extractor or something like an installer; but I digress.

    Those are the two things in the repo as of right now.

    So anyway, here's the about section in the top of the main.cs.
    Code:
    /*          Basic Updater - Executable Edition
    *             Matthew H - www.MatthewH.in
    *                  License - MIT
    *           
    *  Description: This project itself is meant for educational purposes.
    *      However like it says in the MIT license, I don't care if you
    *      use it as long as you 1. don't hold me liable and 2. give me
    *      the proper credits.
    *
    *  Usage: Basically, you can use this when you need to update your
    *      single executable project/program and you would like to do
    *      it so there's little work for your users.
    *      Please keep in mind though, once you release this updater
    *      with your project/program
    *   
    *  Key Features:
    *      - Overwrites the old file
    *      - Random strings for your users to read (much like a quick news)
    *      - Download speed
    *      - Download percentage (%)
    *   
    *  Need Help?
    *      I try to document as much as I can, but everyone could
    *      use some extra help at some point!
    *      Well feel free to hit me up anywhere really. I'm usually on
    *      se7ensins.com as Coldfire202.
    *      Or you could join me on my community forums built for this.
    *      Head over to www.community.matthewh.in
    *   
    *  Notes:
    *      - I place a lot of logging in comments so if you need to
    *          debug you can uncomment the console logs. This is where
    *          I logged information to make sure things were correct.
    *      - Keep in mind, this version is an early version of an
    *          updater I actually use. So the code is not very
    *          "clean" and probably not the most efficient code in the
    *          world.
    */
    

    If you would like something added, or there's an error (by my work...not yours) then please make an issue on there or post on here.
    If you're having issues or anything else, post here.
     
    • Like Like x 1
    Last edited: Apr 17, 2015
  2. Chris7S

    Chris7S Nerd by definition, programmer by trade

    Messages:
    1,508
    Ratings:
    474
    Nice, clean and fully functional. I love the approach you took to updating an application. I mainly used a text file hosted on my sever to compare with the build version of the application. It isn't the best approach but hey, I am not releasing commercial grade software when creating modding tools and such so if it works, it works. Again, great job man!
     
  3. OP
    MatthewH

    MatthewH Member

    Messages:
    1,612
    Ratings:
    613
    Update: Added an example application that looks for an update. Be sure to check it out if you don't know how to implement this updater into your project.

    Thank you Chris.
     
  4. Mr Smithy x

    Mr Smithy x Enthusiast

    Messages:
    328
    Ratings:
    180
    Looks great and it looks clean, json is my favorite format too. just to add, i would usually remove the event handler on download complete.
     
  5. OP
    MatthewH

    MatthewH Member

    Messages:
    1,612
    Ratings:
    613
    If you looked in the code, there's multiple reasons. One if you want to open up the new application, you can use that handler to open it up. And it switches the task. It will come in handy when I add the zip updater later on. The download complete handle is always useful to have.
     
  6. Mr Smithy x

    Mr Smithy x Enthusiast

    Messages:
    328
    Ratings:
    180
    Yeah i see what you mean, i looked back at it and noticed the object isn't global so it wouldn't matter
     
  7. OP
    MatthewH

    MatthewH Member

    Messages:
    1,612
    Ratings:
    613
    Nope, I never try to set global objects as that is a bad habit for anyone to get into.
     
  8. Mr Smithy x

    Mr Smithy x Enthusiast

    Messages:
    328
    Ratings:
    180
    Only a bad habit if you don't know how to utilize it properly; ie. creating a new instance each time consumes memory which is a bad habit
     
  9. Im4eversmart

    Im4eversmart The hacks are real

    Messages:
    2,098
    Ratings:
    1,669
    Good thing about using large 3rd party sites is they are free, high uptime, ddos protected, and have distributed cdn servers.

    Paste JSON formatted text into pastebin, and use that for hosting your file.
     
  10. OP
    MatthewH

    MatthewH Member

    Messages:
    1,612
    Ratings:
    613
    It doesn't matter how you host the file. But using strings and only strings in a PasteBin is unreliable and is not reasonable.
     
    • Like Like x 1
  11. Im4eversmart

    Im4eversmart The hacks are real

    Messages:
    2,098
    Ratings:
    1,669
    Using JSON for one variable is unreasonable to me. It's meant more for serializing whole objects. A plain text protocol for this is sufficient.

    Choosing a solid host is a wise move, since unresponsive servers lead to bad errors if you don't handle it.
     
  12. Visual Studio

    Visual Studio The Original Shiba Inu

    Messages:
    2,766
    Ratings:
    1,342
    A good thing to do is use GetVersionInfo on the old executable and check the version against the json file on the server. Given that something like that would not make it a "simple updater" but it would save the client a lot of time so that they don't download the same file over and over again regardless of version.
     
  13. OP
    MatthewH

    MatthewH Member

    Messages:
    1,612
    Ratings:
    613
    This was originally (in the first versions back in November) how I did it. I soon realized though, this is a bad way.
    Think about it.
    An integer is 123 for example, a float is 1.23. However, floats tend to be rounded. Okay, let me start differently.
    Json doesn't accept floats very well and you would use a lot of brute force just to get things to compare.
    Now think about how version number increase, most people use semantic versioning. Which in my opinion is a good way of doing things.
    x.y.z is our version.
    X: Major patching, breaks backwards compatibility, etc.
    Y: Minor patching, updates that still allow backwards compatibility.
    Z: Bug fixes, patches.

    Now something like
    1.2.5 can not be converted easy for comparison.


    Sorry if that seems like, scatter brained I just got home from work.
    However, it's easier to increment a build number than it is to do versions and such.
     
  14. Visual Studio

    Visual Studio The Original Shiba Inu

    Messages:
    2,766
    Ratings:
    1,342
    Store it as a string then replace the "."'s with a blank char then use int.parse on it, you now have an integer to work with for version. Given that you will have issues with 0's on the beginning but if you want you can remove the leading 0's and then parse it as an int.
     
    Last edited: Apr 19, 2015
  15. OP
    MatthewH

    MatthewH Member

    Messages:
    1,612
    Ratings:
    613
    It's weird. I did that and it always threw an error. I'm not worried. I personally like it better like this anyway.


    However. I just thought about this.
    Semantic versioning. Says that even if you have 1.9.9 and make a minor patch update you don't Change to 2.0.0. You change to 1.9.10. Since I've also made this around that, it adjusts the integer.
    So instead of of 199 it's 1,910.
    So what if you go to 200? It won't updates until 2,000. Which would be 2.0.0.0 which would not follow semantic versioning.

    By all means though. Clone the repo, and add a new project for it and create a pull request. Add all credit you like in there but the whole repo is under the MIT license. So.
     
  16. Visual Studio

    Visual Studio The Original Shiba Inu

    Messages:
    2,766
    Ratings:
    1,342
    You could also try splitting the version string at the .'s and then add them together. The higher the number the more recent it is.
     
    Last edited: Apr 19, 2015
  17. OP
    MatthewH

    MatthewH Member

    Messages:
    1,612
    Ratings:
    613
    Then again. Comes down to
    1.9.9
    And 2.1.0
    1+9+9 = 19
    2+1+0 = 3

    see the problem?
     
  18. Visual Studio

    Visual Studio The Original Shiba Inu

    Messages:
    2,766
    Ratings:
    1,342
    True, didn't think about that, I'll come up with a good solution tonight (I'm just brainstorming ATM).
     
  19. Im4eversmart

    Im4eversmart The hacks are real

    Messages:
    2,098
    Ratings:
    1,669
    Split the version number by period. Check the app version number to the server version number from left to right. If the server is higher first, update app.

    However, you can assume the app is never higher than the server, so if it is different it is lower, and any comparison would work with that.
     
  20. OP
    MatthewH

    MatthewH Member

    Messages:
    1,612
    Ratings:
    613
    To be able to do this would have to do 3 separate checks on each version number because you can't directly compare with the version number because numbers are floats or integers. Floats have one decimal point, not 2 or 3 which would throw back an error. So you can't use logical or arithmetic operations on it.
    So you would essentially be doing 3 or 4 checks (maybe even more) instead of just one.
    In pseudocode.
    Code:
    Get program version number
    Get online version number
    Split both version numbers
    For each number in the version numbers...
    ... Check to see if the internal number is <, ==, > to the online...
    ... if it is greater than (don't see why it would be) or equal to then continue to next number...
    ... if it's not, get the new program information and go to the next function for it.
    
    See? A lot more to do.

    Believe me, I've tried doing other things without a build number.
    However, comparing 2 integers is so much easier than trying to find other ways to check the version number.
     

Share This Page