X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=win%2FC%23%2FfrmDownload.cs;h=8b23df92b9d11db8116cfd6b1a844c05c7cbd725;hb=bdeda20369fa84dc46814378c9a4af6a21ec3212;hp=69c079ab9b697496118b8f25f6676248a8faac3b;hpb=65c22f1b679be4c6a631ea68b501ca465d7ac5c8;p=handbrake-jp%2Fhandbrake-jp-git.git diff --git a/win/C#/frmDownload.cs b/win/C#/frmDownload.cs index 69c079ab..8b23df92 100644 --- a/win/C#/frmDownload.cs +++ b/win/C#/frmDownload.cs @@ -1,15 +1,10 @@ /* frmDownload.cs $ This file is part of the HandBrake source code. - Homepage: . + Homepage: . It may be used under the terms of the GNU General Public License. */ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Text; using System.Windows.Forms; using System.Net; using System.IO; @@ -20,7 +15,7 @@ namespace Handbrake { public partial class frmDownload : Form { - private Thread downloadThread; + private readonly Thread downloadThread; private Stream responceStream; private Stream loacalStream; private HttpWebRequest webRequest; @@ -30,64 +25,59 @@ namespace Handbrake private delegate void DownloadCompleteCallback(); private delegate void DownloadFailedCallback(); + private string file; - public frmDownload() + public frmDownload(string filename) { InitializeComponent(); - try - { - downloadThread = new Thread(Download); - downloadThread.Start(); - } - catch (Exception exc) - { - MessageBox.Show("An error occured on the Download Thread \n" + exc.ToString(),"Error",MessageBoxButtons.OK, MessageBoxIcon.Error); - } - + file = filename; + downloadThread = new Thread(Download); + downloadThread.Start(); } private void Download() { - Functions.RssReader rssRead = new Functions.RssReader(); - - string appPath = Application.StartupPath.ToString() + "\\"; - string hbUpdate = rssRead.downloadFile(); - string downloadPath = appPath + "Handbrake-win.exe"; + string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"); + string hbUpdate = file; WebClient wcDownload = new WebClient(); - try - { - webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate); - webRequest.Credentials = CredentialCache.DefaultCredentials; - webResponse = (HttpWebResponse)webRequest.GetResponse(); - Int64 fileSize = webResponse.ContentLength; - - responceStream = wcDownload.OpenRead(hbUpdate); - loacalStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write, FileShare.None); - - int bytesSize = 0; - byte[] downBuffer = new byte[2048]; - - long flength = 0; - while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0) - { - loacalStream.Write(downBuffer, 0, bytesSize); - flength = loacalStream.Length; - this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize }); - } - - responceStream.Close(); - loacalStream.Close(); - - if (flength != fileSize) - this.Invoke(new DownloadFailedCallback(this.downloadFailed)); - else - this.Invoke(new DownloadCompleteCallback(this.downloadComplete)); - } - catch (Exception) + + try + { + if (File.Exists(tempPath)) + File.Delete(tempPath); + + webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate); + webRequest.Credentials = CredentialCache.DefaultCredentials; + webResponse = (HttpWebResponse)webRequest.GetResponse(); + Int64 fileSize = webResponse.ContentLength; + + responceStream = wcDownload.OpenRead(hbUpdate); + loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None); + + int bytesSize; + byte[] downBuffer = new byte[2048]; + + long flength = 0; + while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0) { - // Do Nothing + loacalStream.Write(downBuffer, 0, bytesSize); + flength = loacalStream.Length; + Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize }); } + + responceStream.Close(); + loacalStream.Close(); + + if (flength != fileSize) + Invoke(new DownloadFailedCallback(this.downloadFailed)); + else + Invoke(new DownloadCompleteCallback(this.downloadComplete)); + } + catch (Exception) + { + // Do Nothing + } } private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes) @@ -101,7 +91,7 @@ namespace Handbrake } catch (Exception exc) { - MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Integer Conversion Error On Download \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -110,14 +100,10 @@ namespace Handbrake lblProgress.Text = "Download Complete"; btn_cancel.Text = "Close"; - string appPath = Application.StartupPath.ToString() + "\\"; - - Process hbproc = Process.Start(appPath + "Handbrake-win.exe"); - hbproc.WaitForExit(); - hbproc.Dispose(); - hbproc.Close(); - + string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"); + Process.Start(tempPath); this.Close(); + Application.Exit(); } private void downloadFailed() @@ -128,13 +114,20 @@ namespace Handbrake private void btn_cancel_Click(object sender, EventArgs e) { - webResponse.Close(); - responceStream.Close(); - loacalStream.Close(); - downloadThread.Abort(); - progress_download.Value = 0; - lblProgress.Text = "Download Stopped"; - this.Close(); + try + { + webResponse.Close(); + responceStream.Close(); + loacalStream.Close(); + downloadThread.Abort(); + progress_download.Value = 0; + lblProgress.Text = "Download Stopped"; + this.Close(); + } + catch (Exception) + { + // Do nothing + } } } } \ No newline at end of file