protected void InstallViewer() { //var downloadViewer = new DownloadViewer("http://instructions/"); //downloadViewer.Show(); //Action performStep = () => downloadViewer.Invoke( // delegate { // downloadViewer.progressBar1.PerformStep(); // }); DownloadViewer.InvokeDelegate performStep = delegate { }; string path = Path.Combine(Path.GetTempPath(), "SL_Setup.exe"); // string downloadUrl = "http://download-secondlife-com.s3.amazonaws.com/Second_Life_1-19-1-4_Setup.exe"; string downloadUrl = "http://get.secondlife.com/"; int bytes = DownloadFile(downloadUrl, path, performStep); // downloadViewer.Close(); if (bytes > 0) { Process slInstallProcess = null; var slInstallPsi = new ProcessStartInfo(); slInstallPsi.FileName = Path.GetFileName(path); slInstallPsi.WorkingDirectory = Path.GetDirectoryName(path); slInstallProcess = Process.Start(slInstallPsi); if (slInstallProcess != null) { slInstallProcess.WaitForExit(); } } }
internal int DownloadFile(String remoteFilename, String localFilename, DownloadViewer.InvokeDelegate performStep) { // Function will return the number of bytes processed // to the caller. Initialize to 0 here. var bytesProcessed = 0; // Assign values to these objects here so that they can // be referenced in the finally block Stream remoteStream = null; Stream localStream = null; WebResponse response = null; // Use a try/catch/finally block as both the WebRequest and Stream // classes throw exceptions upon error try { // Create a request for the specified remote file name var request = (HttpWebRequest)WebRequest.Create(remoteFilename); //request.AllowAutoRedirect = false; request.Referer = "http://www.sunet.se/"; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)"; // Send the request to the server and retrieve the // WebResponse object response = request.GetResponse(); if (response != null) { // Once the WebResponse object has been retrieved, // get the stream object associated with the response's data remoteStream = response.GetResponseStream(); // Create the local file localStream = File.Create(localFilename); // Allocate a 16k buffer var buffer = new byte[16 * 1024]; int bytesRead; // Simple do/while loop to read from stream until // no bytes are returned do { // Read data (up to 1k) from the stream bytesRead = remoteStream.Read(buffer, 0, buffer.Length); // Write the data to the local file localStream.Write(buffer, 0, bytesRead); // Increment total bytes processed bytesProcessed += bytesRead; } while ((bytesRead > 0) && (!CancelDownloadViewer)); } } catch (Exception e) { Console.WriteLine(e.Message); } finally { if (response != null) { response.Close(); } if (remoteStream != null) { remoteStream.Close(); } if (localStream != null) { localStream.Close(); } } return(bytesProcessed); }