First, first of all, let's talk about Launcher's core features---download function.
This part of the estimated good several to write, things more and more miscellaneous, slowly, my things are also continuing to improve ...
Downloading a piece from the Web requires several classes, HttpWebRequest (HTTP request), HttpWebResponse (http reply), stream (file stream), and the approximate process is to create a request first and then wait for the Web server to answer. At this time it will go to find this URL exists, if not exist will be error, so it is best to put in the Try/catch block to prevent the program from collapsing. Then you can create a Web request flow file. Note that there are two stream files to create, one to get answers from the web, and one to create a local file to write the data that you requested, and then wait for it to get the data. It is important to note that because it is a file operation, the file is opened once to close once, be sure to close must be off be sure to close the key things to say three times, if you do not close the words will appear data loss.
Specific on the code bar, because I myself also, look at People's blog long-winded without code, the result is still not ~ ~ ~
HttpWebRequest MYRQT = (HttpWebRequest) httpwebrequest.create (URL); HttpWebResponse Myrpe = (HttpWebResponse) myrqt.getresponse (); TotalBytes = Myrpe.contentlength; The total number of bytes received from the Web response, this is to make progress bar Stream st = Myrpe.getresponsestream (); Create flow stream from Web request so = new FileStream (filePath + "\ \" + FileName, FileMode.Create);//create Write file stream Byte[] by = new byte[1024]; int osize = St. Read (by, 0, (int.) by. Length); while (Osize > 0) {so. Write (by, 0, osize); Osize = St. Read (by, 0, (int.) by. Length); } so. Close (); St. Close ();
Now a file download function is done, but we can not only download a file, it is impossible to write all the URL of the file and then request to download it, so if the Web server added new files, the entire launcher will have to re-change ... This involves the configuration file we said earlier, the configuration file has two fields is necessary, one is the path of the file, plus your Web server URL can become, one is MD5 code.
So now we're going to jump to the build of the configuration file?! It seems to be a bit messy and seems to have a logical appearance ~
All right, let's talk about the configuration file first.
I use the configuration file is an XML file, of course you can also use TXT AH Excel table Ah, just write method and read method is not the same, I only speak XML file, others to explore it yourself. (actually because of the other I have no use to know ~ ~ ~)
We need to use two Namespace,system.xml and System.IO, OK, as long as you have the file operation must use System.IO this namespace. Now let's assume that all the files you want to upload to the Web server are all in one folder, what? Not under a folder? Then put it together, why bother yourself ... We call this file the total folder for the time being.
Specifically how to create I will not write, mainly in the process of writing you need to traverse all the files in the folder contains its sub-files, which requires recursion, to find a file after the creation of a node, Then put its fileinfo.directoryname as a field of your node into your XML file, and then calculate its MD5 code as another field is placed under this node, and then you can see the effect.
<file size= "309" md5= "ca9e7a2df6c52c9b48b76fb6e232f651" name= "D:\myfile\AssetBundleList.txt" > </file>
But then you will find a problem, your path has a section of your total folder absolute path, but we do not need, always will not be uploaded to the Web path also bring a disk directory, so we need to remove this paragraph. Everyone must say that the direct replace into empty is good, but there is a problem, if your total folder path is directly written in the code (for example, D:\\myfile), the path in your XML file will become D:\myfile, so you directly remove the words he will not find So you have to replace "\" with "\" (It is a signifier, to make it into a character will add @), and then remove, "//" Also, to convert to @ "\", but you can also try to pass the "D:\mfile", I have not tried, perhaps feasible?
Here is not on the code, if not, you can ask me, I will not tell you ~ hahaha, no, if you are a C # programmer, you must be clearer than I, if you are not, degree Niang must be clearer than I ~ But if you are a form program, the path of the total folder is entered from the window, there is no problem, It will identify itself as "\" ...
Here's how to calculate the MD5 code.
public string CalculateMD5 (string filename) //Calculate MD5 code, parameter is file name, return value is MD5 code { try { FileStream file = New FileStream (FilePath + "//" + FileName, FileMode.Open); System.Security.Cryptography.MD5 MD5 = new System.Security.Cryptography.MD5CryptoServiceProvider (); byte[] RetVal = md5.computehash (file); File. Close (); StringBuilder sb = new StringBuilder (); for (int i = 0; i < retval.length; i++) { sb. Append (Retval[i]. ToString ("X2")); } Return SB. ToString (); } catch (Exception ex) { throw new Exception ("Getmd5hashfromfile () Fail,error:" + ex. Message); } }
OK, this one is here, and we'll go on to say it next.
Reprint Please specify source: http://www.cnblogs.com/shellxi/p/5553566.html
Micro-end Game Launcher Launcher (series one)