public bool Download()
{
bool success = false;
try
{
if (!Directory.Exists(Application.StartupPath + @"\Update"))
{
Directory.CreateDirectory(Application.StartupPath + @"\Update");
}
string url = "http://www.andwho.com/AndwhoUpdate/DeskHelper/DeskHelper.xml";
Uri uri = new Uri(url);
HttpWebRequest mRequest = (HttpWebRequest)WebRequest.Create(uri);
mRequest.Method = "GET";
mRequest.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse mResponse = (HttpWebResponse)mRequest.GetResponse();
Stream sIn = mResponse.GetResponseStream();
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
long length = mResponse.ContentLength;
long i = 0;
while (i < length)
{
byte[] buffer = new byte[length];
int read = sIn.Read(buffer, 0, buffer.Length);
i += read;
string strTemp = System.Text.Encoding.UTF8.GetString(buffer, 0, read);
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strTemp);
fs.Write(bytes, 0, bytes.Length);
}
sIn.Close();
mResponse.Close();
fs.Close();
success = true;
}
catch
{
}
return success;
}
//检查更新 private void UpdateToolStripMenuItem_Click(object sender, EventArgs e) { try { _checkUpdate = new CheckUpdate(); if (_checkUpdate.IsConnectedInternet()) { if (_checkUpdate.Download()) { if (_checkUpdate.HasNewVersion()) { form_Update = UpdateForm.GetInstance(_checkUpdate.NewVersion); form_Update.CloseHandlor += new UpdateForm.CloseHandler(CloseDeskHelper); form_Update.ShowForm(); } else//HasNewVersion() { MessageBox.Show("已经是最新版本"); } } else//Download() { MessageBox.Show("已经是最新版本"); } } else//IsConnectedInternet() { MessageBox.Show("本机没有连接互联网"); } } catch (Exception ex) { log.WriteLog(ex.ToString()); } }