本人想做一个自动升级程式,在C#程序中调外部升级程式,并闭自己,但运行时出错。//启动外部程式
string mFilePath = Application.StartupPath + "\\SysUpdate.exe";
Process.Start(mFilePath);//关闭自己
frmS_Main_FormClosing(null, null);

解决方案 »

  1.   

    Hide隐藏
    http://topic.csdn.net/u/20090422/08/69f9713b-3982-4313-ab11-7fb6eefdcf93.html
      

  2.   

    学习了
    http://www.zhenrenbar.org
      

  3.   

    还有一种是shell的exec方式     [DllImport("shell32.dll")]   
          
    public     static     extern     int     ShellExecute(IntPtr     hwnd,StringBuilder     lpszOp,StringBuilder     lpszFile,StringBuilder     lpszParams,StringBuilder     lpszDir,int     FsShowCmd);         
      
    调用:   
          ShellExecute(IntPtr.Zero,new     StringBuilder("Open"),new     StringBuilder("notepad"),new     StringBuilder(""),new     StringBuilder(@"C:"),1);   
      

  4.   

    另:怎样在C#程式中获取到当前应用程式的名称(如:当前运行的是PMC.exe,应知道这个名称)
      

  5.   

    退出当前的程序
    Application.ExitThread();
      

  6.   

    其实LZ要做的是升级程序,监视程序监视网络新版本,如果有,则关闭主程序 kill掉
    然后下载新的文件覆盖老的文件,覆盖后再start它 监视程序完全可以不去关闭而一直监视
      

  7.   

            private void button1_Click(object sender, EventArgs e)
            {
                //string mFilePath = Application.StartupPath + "\\SysUpdate.exe";
                string mFilePath = "d:\\2.exe";          System.Diagnostics.Process pc=  Process.Start(mFilePath);
              while (pc.Handle == IntPtr.Zero)
              {
                  Application.DoEvents();
              }
              this.Close();        }
      

  8.   

    晕,我这怎么没有错        private void button2_Click(object sender, EventArgs e)
            {
                Process p = Process.Start("WindowsFormsApplication7.exe");
                this.Close();
                //Application.Exit();
            }
      

  9.   

    解耦合的自动更新核心代码。自己看,不解释。[STAThread]
            static void Main(string[] args)
            {
                bool flag = false;
                Assembly assm = typeof(Program).Assembly;
                Mutex mutex = new Mutex(true, ((GuidAttribute)(assm.GetCustomAttributes(typeof(GuidAttribute), false)[0])).Value, out flag);
                if (!flag)
                {
                    MessageBox.Show("已经存在系统,无法再启动!");
                }
                else
                {
                    
                    if (IsServerActive())
                    {
                        //自动更新
                        SyscUpdateModule();
                        if (IsVersionAvailable() == DialogResult.Yes)
                        {
                            Update();
                        }
                        else
                        {
                            StartApp();
                        }
                    }
                    else
                    {
                        StartApp();
                    }
                }
            }
            static void StartApp()
            {
               
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                FrmLogin frmLogin = new FrmLogin();
                if (frmLogin.ShowDialog() == DialogResult.OK)
                {                
                    FrmMain frmMain = new FrmMain();
                    Application.Run(new FrmMain());                
                }           
            }        static bool IsServerActive()
            {
                string assemFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Updater.exe");
                Assembly assem = Assembly.LoadFrom(assemFile);
                Type type = assem.GetType("Updater.UpdaterModule");
                Object instant = Activator.CreateInstance(type);
                MethodInfo miCheck = type.GetMethod("IsServerActive");
                return (bool)miCheck.Invoke(instant, null);
            }        static DialogResult IsVersionAvailable()
            {
                string assemFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Updater.exe");
                Assembly assem = Assembly.LoadFrom(assemFile);
                Type type = assem.GetType("Updater.UpdaterModule");
                Object instant = Activator.CreateInstance(type);
                MethodInfo miUpdate = type.GetMethod("IsVersionAvailable");
                return (DialogResult)miUpdate.Invoke(instant, null);
            }        static void Update()
            {
                Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Updater.exe"));
                Application.Exit();
            }        /// <summary>
            /// 更新更新文件
            /// </summary>
            static void SyscUpdateModule()
            {
                try
                {
                    string localFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Updater.exe");
                    FileInfo localFI = new FileInfo(localFile);
                    XPathDocument doc = new XPathDocument(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Updater.xml"));
                    XPathNavigator nav = doc.CreateNavigator();
                    string updatePath = nav.SelectSingleNode("/Updater/UpdatePath").Value;
                    string remoteFile = GetUri(updatePath, "Updater.exe");
                    FileInfo remoteFI = DownloadHttpFile(remoteFile);
                    if (remoteFI.LastWriteTime > localFI.LastWriteTime || localFI.Length != remoteFI.Length)
                    {
                        File.Copy(remoteFI.FullName, localFile, true);
                    }
                }
                catch (Exception)
                {
                }
            }        static FileInfo DownloadHttpFile(string url)
            {
                Uri uri = new Uri(url);
                string localFile = Path.Combine(GetTempDir(), Path.GetFileName(uri.AbsolutePath));            WebClient wc = new WebClient();
                wc.DownloadFile(url, localFile);
                return new FileInfo(localFile);
            }        static string GetUri(string uri, string filename)
            {
                return uri.Trim('/') + "/" + filename.Trim('/');
            }        static string GetTempDir()
            {
                string dir = Path.GetTempPath();
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);
                return dir;
            }        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
            {
                Form currentForm = null;
                if (null != Application.OpenForms && 0 != Application.OpenForms.Count)
                {
                    currentForm = Application.OpenForms[0];
                }
                Exception ae = e.ExceptionObject as Exception;
                if (null != ae)
                {
                    if (null == currentForm)
                    {                    MessageBox.Show(ae.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                    else
                    {
                        MessageBox.Show(currentForm, ae.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
            }
      

  10.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    using System.Xml;
    using System.Reflection;
    using System.Net;namespace Updater
    {
        public sealed class UpdaterModule
        {
            private static string updaterConfig = "Updater.xml";
            public static string ProductName { get; set; }
            public static string UpdatePath { get; set; }
            public static string Version { get; set; }
            public static bool NeedUpdate { get; set; }
            public static bool Debug { get; set; }        static UpdaterModule()
            {
                string file = Path.Combine(Application.StartupPath, updaterConfig);
                XmlDocument doc = new XmlDocument();
                doc.Load(file);
                UpdatePath = doc.SelectSingleNode("/Updater/UpdatePath").InnerText;
                ProductName = doc.SelectSingleNode("/Updater/ProductName").InnerText;
                Version = doc.SelectSingleNode("/Updater/Version").InnerText;
                NeedUpdate = bool.Parse(doc.SelectSingleNode("/Updater/NeedUpdate").InnerText);
                Debug = bool.Parse(doc.SelectSingleNode("/Updater/Debug").InnerText);
                if (Debug)
                {
                    System.Diagnostics.Debugger.Break();
                }
            }        public bool IsServerActive()
            {
                return IsServerActiveInternal(UpdatePath);
            }        static string GetUri(string uri, string filename)
            {
                return uri.Trim('/') + "/" + filename.Trim('/');
            }        private bool IsServerActiveInternal(string updatePath)
            {
                try
                {
                   HttpWebRequest request = HttpWebRequest.Create(GetUri(updatePath, updaterConfig)) as HttpWebRequest;
                   request.Timeout = 3000;
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.OK)
                        return true;
                    return false;
                }
                catch (Exception)
                {
                    return false;
                }
            }        string GetTempDir()
            {
                string dir = Path.GetTempPath();
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);
                return dir;
            }        public DialogResult IsVersionAvailable()
            {
                WebClient wc = new WebClient();
                string newLocalUpdateConfig = Path.Combine(GetTempDir(), updaterConfig);
                wc.DownloadFile(GetUri(UpdatePath, updaterConfig), newLocalUpdateConfig);
                string newVersion = null;
                XmlDocument doc = new XmlDocument();
                doc.Load(newLocalUpdateConfig);
                newVersion = doc.SelectSingleNode("/Updater/Version").InnerText;
                if (VersionCompare(Version,newVersion) < 0)
                {
                    ////需要更新
                    //if (MessageBox.Show(string.Format("{0}发现新版本!\n当前版本:{1}\n新版本:{2}\n是否更新?(Y/N?)",
                    //    ProductName, Version, newVersion), "新版本可用",
                    //    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    //{
                        Version = newVersion;
                        NeedUpdate = true;
                        WriteUpdateConfig();
                        return DialogResult.Yes;
                    //}
                    //else
                    //{
                    //    return DialogResult.No;
                    //}
                }
                return DialogResult.Cancel;
            }        private int VersionCompare(string oldVersion, string newVersion)
            {
                Version version1 = new Version(oldVersion);
                Version version2 = new Version(newVersion);
                return version1.CompareTo(version2);
            }        internal static void WriteUpdateConfig()
            {
                string file = Path.Combine(Application.StartupPath, updaterConfig);
                XmlDocument doc = new XmlDocument();
                doc.Load(file);
                doc.SelectSingleNode("/Updater/UpdatePath").InnerText = UpdatePath;
                doc.SelectSingleNode("/Updater/ProductName").InnerText = ProductName;
                doc.SelectSingleNode("/Updater/Version").InnerText = Version;
                doc.SelectSingleNode("/Updater/NeedUpdate").InnerText = NeedUpdate.ToString();
                doc.Save(file);
            }
        }
    }
      

  11.   


    <?xml version="1.0" encoding="utf-8"?>
    <Updater>
      <!--服务器更新路径-->
      <UpdatePath>http://update/update</UpdatePath>
      <ProductName>XXXX系统</ProductName>
      <MainExe>XXXXXX.exe</MainExe>
      <Version>1.0.0.0</Version>
      <NeedUpdate>False</NeedUpdate>
      <Debug>False</Debug>
      <Files>
        <File Name="app.xml" />
        <File Name="Castle.ActiveRecord.dll" />
        <File Name="Castle.ActiveRecord.xml" />
        <File Name="Castle.Components.Validator.dll" />
        <File Name="Castle.Components.Validator.xml" />
        <File Name="Castle.Core.dll" />
        <File Name="Castle.Core.xml" />
        <File Name="Castle.DynamicProxy.dll" />
        <File Name="Castle.DynamicProxy.xml" />
        <File Name="Iesi.Collections.dll" />
        <File Name="Iesi.Collections.xml" />
        <File Name="log4net.dll" />
        <File Name="log4net.xml" />
        <File Name="NHibernate.dll" />
        <File Name="Updater.xml" />
        <File Name="WeifenLuo.WinFormsUI.Docking.dll" />
        <File Name="zh-CHS\Keyin.Project.DMRS.WinFormsUI.resources.dll" />
      </Files>
    </Updater>
      

  12.   


    //启动外部程式
    string mFilePath = Application.StartupPath + "\\SysUpdate.exe";
    Process.Start(mFilePath);//关闭自己
    Application.Exit();
    ----------
    三易通软件(三易通服装进销存,三易通服装进销存软件,三易通服装进销存管理软件,三易通服装进销存管理系统,三易通服装店软件,三易通服装店管理软件,三易通服装店管理系统,三易通服装销售软件,三易通服装管理软件,三易通服装销售管理软件,三易通服装销售管理系统,三易通服装零售管理软件,三易通服装零售管理系统,三易通服装店收银软件)http://www.3etsoft.cn
      

  13.   

    用个bat完成这个工作岂不更好?
      

  14.   

     static void Main()
            {
                try
                {
                    UpdateManager um = new UpdateManager(DefaultConst.DEFAULT_LOCAL_CFG);
                    if (um.CheckNeedUpdate())
                    {
                        FormUtil.ShowInfo("程序检测到可以更新的组件,将进行下载更新!");
                        Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Update.exe"));
                        // Application.Exit(); 居然还会继续执行下面的内容
                        Environment.Exit(0);
                    }
                }
                catch { }            Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new MainForm());
            }