c#项目在本地vs2008坏境中运行可以解压后台下载的rar压缩包,但把项目制作成安装包后就不能解压。提示找不到压缩包.......跪求指点 .有示例可否发送到546452472@qq。com 谢谢using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Xml;
using System.Collections;
using System.Threading;
using Microsoft.Win32;
namespace UpReport
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
          //  MessageBox.Show(Application.StartupPath.ToString());
            killpro();
            Updates();   //下载所需的更新文件
            if (DoUnPack())
            {
                CopyDirectory(Application.StartupPath + @"\update\", Application.StartupPath);    //用新的文件覆盖旧的文件,删除临时文件夹
                Directory.Delete(Application.StartupPath + @"\update", true);
                File.Delete(Application.StartupPath + @"\update.rar");
                //删除文件
                if (File.Exists(@"C:\AutoUpdater.xml"))
                {
                    File.Delete(@"C:\AutoUpdater.xml");
                    Application.Exit();
                }
            }
        }
        //判断主程序是否运行
        private void killpro()
        {
            System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName("pyReport");
            //关闭原有应用程序的所有进程 
            foreach (System.Diagnostics.Process pro in proc)
            {
                pro.Kill();
            }
        }
        //下载xml文件里的所需下载的文件
        private void Updates()
        {
            string path = "http://192.168.1.101:8012/jjc/updatesoft/";
            string filename;
            string localpath;
            //DownloadFile(path +"AutoUpdater.xml", @"E:\AutoUpdater.xml", progressBar1, label1);            string AutoUpdaterFileName = @"C:\AutoUpdater.xml";
            //打开xml文件 
            FileStream myFile = new FileStream(AutoUpdaterFileName, FileMode.Open);
            //xml文件阅读器 
            XmlTextReader xml = new XmlTextReader(myFile);
            while (xml.Read())
            {
                if (xml.Name == "UpdateFile")
                {
                    filename = path + xml.GetAttribute("FileName");
                   
                    localpath = Application.StartupPath + "//" + xml.GetAttribute("FileName");
                    DownloadFile(filename, localpath, progressBar1, label1);
                }
            }
            xml.Close();
            myFile.Close();
        }
        /// <summary>        
        /// 下载更新文件        
        /// </summary>        
        /// <param name="URL">下载文件地址</param>       
        /// 
        /// <param name="Filename">下载后的存放地址</param>        
        /// <param name="Prog">用于显示的进度条</param>        
        /// 
        public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1)
        {
            float percent = 0;
          
            try
            {
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                if (Myrq == null)
                {
                    MessageBox.Show("未找到更新文件包!");                }
                else
                {
                    System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                   
                   
                    long totalBytes = myrp.ContentLength;
                    if (prog != null)
                    {
                        prog.Maximum = (int)totalBytes;
                    }
                    System.IO.Stream st = myrp.GetResponseStream();
                    System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                    long totalDownloadedByte = 0;
                    byte[] by = new byte[1024];
                    int osize = st.Read(by, 0, (int)by.Length);
                    while (osize > 0)
                    {
                       
                        totalDownloadedByte = osize + totalDownloadedByte;
                        System.Windows.Forms.Application.DoEvents();
                        so.Write(by, 0, osize);
                        if (prog != null)
                        {
                            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(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
                    }
                     
                        so.Close();
                        st.Close();
                      myrp.Close();
                   
               }
            }
            catch (System.Exception)
            {
                throw;
            }
        }
        //解压
        public bool DoUnPack()
        {
            bool r = false;
            string strR = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "update";
            Console.WriteLine("开始解压......");
            RegistryKey reg;
            string rarPath;
            Process process = new Process();            try
            {
                reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
                rarPath = reg.GetValue("").ToString();
                rarPath = rarPath.Substring(1, rarPath.Length - 7);
                process.StartInfo.FileName = rarPath;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                // -0+ 如果已经存在则覆盖
                process.StartInfo.Arguments = string.Format(" x -o+ {0} {1}", strR, Application.StartupPath);//这里是命令行请参考winrar的说明
                //开始解压
                process.Start();
                while (!process.HasExited)
                {
                }
                //进程终止
                if (process.HasExited)
                {
                    int exitCode = process.ExitCode;
                    if (exitCode == 0)
                    {
                        Console.WriteLine("{0} 正常完成", exitCode.ToString());
                        r= true;
                    }
                    else
                    {
                        Console.WriteLine("{0} 正常完成", exitCode.ToString());
                        
                    }
                }
                Console.WriteLine("解压完成");
                return r;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //释放资源
                process.Close();
                
            }
        }        //将下载的升级文件覆盖到原来的程序
        /// <summary>
        /// 拷贝文件夹
        ///By Wang Hw  www.pegete.com.cn 
        /// </summary>
        /// <param name="srcdir"></param>
        /// <param name="desdir"></param>
        private void CopyDirectory(string srcdir, string desdir)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);            string desfolderdir = desdir + "\\" + folderName;
           // MessageBox.Show(desdir);//E:\Pwc\UpReport\UpReport\bin\Debug
           // MessageBox.Show(srcdir);//E:\Pwc\UpReport\UpReport\bin\Debug\update\
         
            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);            foreach (string file in filenames)// 遍历所有的文件和目录
            {
               // MessageBox.Show(file);
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {
                    //MessageBox.Show("1" + file);
                    string currentdir = desfolderdir + file.Substring(file.LastIndexOf("\\") + 1) + "\\";
                  
                 //   MessageBox.Show(currentdir+"1");
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }
                    CopyDirectory(file, currentdir);
                }                else // 否则直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);
                  //  MessageBox.Show(desdir + "\\" + srcfileName + "2");
                    //srcfileName = desfolderdir + srcfileName;                   // MessageBox.Show("1" + srcfileName);
                    //if (!Directory.Exists(desfolderdir))
                    //{
                    //    Directory.CreateDirectory(desfolderdir);
                    //}
                    if (File.Exists(desdir + "\\" + srcfileName))
                    {
                        File.Delete(desdir + "\\" + srcfileName);
                    }
                    File.Copy(file, desdir + "\\" + srcfileName);
                }
            }//foreach        }//function end
    }
}

解决方案 »

  1.   

    有个autoupdate 的开源代码 我记得 你搜索下呗
    你这个代码是winform的? 调试下啊 rar不能解压什么意思 是下载后不能解压 你代码调用对了么
    还是少引用 解压rar的组件?
      

  2.   

    是winform的。。   关键是在vs2008 环境下运行 可以到网站上下载更新包。 也可以解压更新包 没有任何问题。 但打包后只能下载更新包 不能解压  提示找不到压缩文件。