![]() |
|
[C#] AutoUpdate.cs | Automatically update your application+++ - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] AutoUpdate.cs | Automatically update your application+++ (/showthread.php?tid=5813174) |
AutoUpdate.cs | Automatically update your application+++ - gigajew - Mar 18, 2018 This is a more 'stable' auto-updater. It doesn't rely on timing or batch scripts, it simply renames itself and downloads the new application from the server, then restarts itself using Application.Restart (which still has the cached filename). Remember to reference System.Windows.Forms.dll. Usage: Code using System;RE: AutoUpdate.cs | Automatically update your application+++ - glitchgxd - Mar 19, 2018 Instead of renaming the current to an ".old", download to a ".download.tmp" first, validate the file after download completion, and then do the swap. That way you cover events where the user might drop connection or something. Otherwise, looks clean. RE: AutoUpdate.cs | Automatically update your application+++ - Ninja - Mar 19, 2018 I have a few remarks on the code: Why do this: Code // parse the responseInstead of: Code using(HttpWebResponse response = request.GetResponse() as HttpWebResponse)- You don't have to set the request.Proxy property to default(WebProxy). Null is a perfectly fine value (and according to some can speed up the requests a little bit as well). - Why not use the keywords for primitive types? I keep seeing "Double" and "Int32" and stuff like that, but why not just use "double", "int", etc. - The default WebRequest.Method value is GET, so no need to set it again. - I see you check if the status is in the "OK spectrum" as you call it. If a web server wants to let you know all went fine it will just use a OK 200 status code. So you can just check for that. This whole line of code gives me aids: Code if (Math.Floor(((Double)((Double)response.StatusCode) / 100)) == 2.0)RE: AutoUpdate.cs | Automatically update your application+++ - gigajew - Mar 19, 2018 default(WebProxy) yields null and is technically a more correct way of nullifying values. Also as far as the try-catch goes, if you call WebRequest.GetResponse() and it has another status code than 200 it will throw an exception and you can instead collect the response object from the catch. It's really odd behavior. Also the getEncoding is not actually used and could be omitted from the code. About 4-5% of websites use it, where others use UTF-8 RE: AutoUpdate.cs | Automatically update your application+++ - gigajew - Mar 19, 2018 (Mar 19, 2018 10:35 AM)Orka Wrote: How come you're forcing a garbage collection as part of the restart? Your file/network handling doesn't allow for trailing unmanaged resources. It's probably just a mistake. I can't remember RE: AutoUpdate.cs | Automatically update your application+++ - n0$f3ratu$ - Mar 19, 2018 What if the application has dependencies? What if those dependencies are placed in different folders? RE: AutoUpdate.cs | Automatically update your application+++ - Ninja - Mar 19, 2018 (Mar 19, 2018 10:34 AM)gigajew Wrote: Also as far as the try-catch goes, if you call WebRequest.GetResponse() and it has another status code than 200 it will throw an exception and you can instead collect the response object from the catch. It's really odd behavior. I know, I never said you should not use a try catch. I was wondering as to why you did not use a using statement. I like to use an extension method that has the try catch in it to return the WebResponse object no matter if an exception was thrown. Then you could just do: Code using(HttpWebResponse response = request.GetResponse(ignoreException: true) as HttpWebResponse)(Mar 19, 2018 10:34 AM)gigajew Wrote: default(WebProxy) yields null and is technically a more correct way of nullifying values How is it technically more correct? The only way I see it being 'better' is when using generics. RE: AutoUpdate.cs | Automatically update your application+++ - Aristocles - Mar 19, 2018 Cool thank you. Ever thought about a smart update process ? Instead of downloading the updated file as a whole, maybe you can download only the new byte patterns of the file in hex. I'm not sure I'm being very clear here but that would make the updating process quicker. RE: AutoUpdate.cs | Automatically update your application+++ - Ninja - Mar 19, 2018 (Mar 19, 2018 03:47 PM)Martin Heidegger Wrote: Cool thank you. Ever thought about a smart update process ? Instead of downloading the updated file as a whole, If I understand you correctly then it is the same as the "smart" remote desktop thread not too long ago, where a user only wanted to send the changed bytes over. If I recall correctly he used XOR or something. So it is definitely doable. RE: AutoUpdate.cs | Automatically update your application+++ - Aristocles - Mar 19, 2018 (Mar 19, 2018 05:14 PM)Ninja Wrote: If I understand you correctly then it is the same as the "smart" remote desktop thread not too long ago, where a user only wanted to send the changed bytes over. If I recall correctly he used XOR or something. So it is definitely doable. You got me right |