日期:2014-05-18  浏览次数:20798 次

WM 下载程序判断文件是否存在
try
  {
  HttpWebRequest Myrq = (HttpWebRequest)HttpWebRequest.Create(URL);
  HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse();
  //判断是否有这个文件,如果有这个文件就删除
  if (Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(Filename + "\\client.cab")) == true)
  {
  Directory.Delete(System.Web.HttpContext.Current.Server.MapPath(Filename + "\\client.cab"), true);
  }
  long totalBytes = myrp.ContentLength;
  if (Prog != null)
  {
  Prog.Maximum = (int)totalBytes;
  }
  System.IO.Stream st = myrp.GetResponseStream();
  System.IO.Stream so = new FileStream(Filename, FileMode.Append, FileAccess.Write);
  long totalDownloadedByte = 0;
  byte[] by = new byte[1024];
  int osize = st.Read(by, 0, (int)by.Length);
  while (osize > 0)
  {
  totalDownloadedByte = osize + totalDownloadedByte;
  Application.DoEvents();
  so.Write(by, 0, osize);
  Prog.Value = (int)totalDownloadedByte;
  osize = st.Read(by, 0, (int)by.Length);
  percent = (float)totalDownloadedByte / (float)totalBytes * 100;
  label1.Text = "更新进度" + percent.ToString() + "%";
  System.Windows.Forms.Application.DoEvents();
  }
  so.Close();
  st.Close();
  MessageBox.Show("下载完成", "下载提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
  System.Diagnostics.Process.Start("client.cab", Filename);
  Application.Exit();
  }

执行下面这就死了呢

 //判断是否有这个文件,如果有这个文件就删除
  if (Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(Filename + "\\client.cab")) == true)
  {
  Directory.Delete(System.Web.HttpContext.Current.Server.MapPath(Filename + "\\client.cab"), true);
 
 
 


------解决方案--------------------
试试这样
C# code

string path = System.IO.Path.GetFullPath(System.Web.HttpContext.Current.Server.MapPath("~/client.cab"));
if(File.Exists(path)))
{
    File.Delete(path))
}