惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - -==NoWay.==-

[转载]使用C#的BitmapData The Open Source iPhone Apps List Lambda印象 使用WinDBG + SOS调试.Net程序的一般步骤 获取系统信息 魔兽、星际和红警的比较 迟到 年华 一款简单却很非脑力的小游戏 Implementing Virtual Mode with Just-In-Time Data Loading in the Windows Forms DataGridView Control 只因女婿是VB程序员,刚见面就被未来岳父轰出家门 About System.Reflection.Emit InitialInstanceActivator AsyncCallback The Windows Control 为开通http://beta.zooomr.com/的Pro帐户用的 AsyncCallback The Windows Control 标准代码页列表 winmine cheat Read MP3 Header Info
一个自动更新的简单实现(通过反射解耦) - -==NoWay.==- - 博客园
-==NoWay.==- · 2011-02-15 · via 博客园 - -==NoWay.==-

更新模块的代码:

 代码

更新模块的UI:

代码

    public partial class FrmMain : Form
    {
        
public FrmMain()
        {
            InitializeComponent();
        }

        XmlDocument doc 

= new XmlDocument();#region "Disable the 'X'"
        
protected override CreateParams CreateParams
        {
            
get
            {
                CreateParams cp 
= base.CreateParams;
                
const int CS_NOCLOSE = 0x200;
                cp.ClassStyle 
= cp.ClassStyle | CS_NOCLOSE;
                
return cp;
            }
        }
        
#endregionprivate void FrmMain_Load(object sender, EventArgs e)
        {
            
string config = Path.Combine(Path.GetTempPath(),"Updater.xml");
            doc.Load(config);
            
this.lblProduct.Text = doc.SelectSingleNode("/Updater/ProductName").InnerText;
            
this.lblNewVer.Text = "new version:" + doc.SelectSingleNode("/Updater/Version").InnerText;
            
this.lblOriginVer.Text = "old version:" + UpdaterModule.Version;
            MethodInvoker mi 
= new MethodInvoker(UpdateSys);
            mi.BeginInvoke(
new AsyncCallback(Finished),null);
        }
private void Finished(IAsyncResult result)
        {
            
if (result.IsCompleted)
            {
                Process.Start(Path.Combine(Application.StartupPath,doc.SelectSingleNode(
"/Updater/MainExe").InnerText));
                
this.Close();
            }
        }
private delegate void StringDelegate(string info);
        
private delegate void IntDelegate(int value);private void SetInfo(string info)
        {
            
if (this.InvokeRequired)
            {
                
this.Invoke(new StringDelegate(SetInfo), info);
                
return;
            }
            
else
            {
                
this.lblInfo.Text = info;
            }
        }
private void SetTotalValue()
        {
            
if (this.InvokeRequired)
            {
                
this.Invoke(new MethodInvoker(SetTotalValue));
                
return;
            }
            
else
            {
                
this.pbTotal.Value++;
            }
        }
private void SetTotalMax(int value)
        {
            
if (this.InvokeRequired)
            {
                
this.Invoke(new IntDelegate(SetTotalMax), value);
                
return;
            }
            
else
            {
                
this.pbTotal.Maximum = value;
            }
        }
private void UpdateSys()
        {
            
if (UpdaterModule.Debug)
            {
                System.Diagnostics.Debugger.Break();
            }
            
//根据临时文件夹中的文件进行下载           
            WebClient wc = new WebClient();            
            
string updatePath = doc.SelectSingleNode("/Updater/UpdatePath").InnerText;
            XmlNodeList xnl 
= doc.SelectNodes("/Updater/Files/File");
            SetTotalMax(xnl.Count);
            
foreach (XmlNode xn in xnl)
            {                
                
try
                {
                    
string localFile = Path.Combine(Application.StartupPath, xn.Attributes["Name"].Value);
                    
string dir = Path.GetDirectoryName(localFile);
                    
if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
                    SetInfo(
"processing:" + localFile);
                    wc.DownloadFile(GetUri(updatePath, xn.Attributes[
"Name"].Value), localFile);
                }
                
catch (Exception)
                {
                    
if (UpdaterModule.Debug)
                    {
                        Debugger.Break();
                    }
                }
                SetTotalValue();
            }
        }
static string GetUri(string uri, string filename)
        {
            
return (uri.Trim('/'+ "/" + filename.Trim('/')).Replace('\\','/');
        }
        
        
string GetTempDir()
        {
            
string dir = Path.GetTempPath();
            
if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            
return dir;
        }

    }

配置文件:

 代码

调用方:

代码

    static class Program
    {
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>
        [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
            {
//自动更新
                SyscUpdateModule();
                
if (IsServerActive())
                {
                    
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)
            {
                WinFormsHelper.FindContentOrCreate
<FrmMRViewer>().Hide();
                Application.Run(FrmMain.Instance);
                
//Application.Run(new FrmSpecialMR());
            }
        }
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);
                }
            }
        }

    }