使用FTP组件实现程序自动更新

using System;
using EnterpriseDT.Net.Ftp;

namespace HzyModule
{
    
public class PragrammerUpdate
    
{
        FTPClient FTPDownloadClient;
        
public void Update(string LocalPath,FTPClient FTPclient)
        
{
            FTPDownloadClient
=FTPclient;
            update (LocalPath);
        }

        
private void update(string local)
        
{
            
if (!System.IO.Directory .Exists (local)) System.IO .Directory .CreateDirectory (local);
            FTPFile[] RemoteFiles;

            
string[] filename=FTPDownloadClient.Dir ();
            RemoteFiles
=FTPDownloadClient.DirDetails("*.*");
            
int filenumbers=RemoteFiles.Length ;
            
string FileDirectoryName=null;
            
for (int i=0;i<filenumbers;i++)
            
{
                FileDirectoryName
=RemoteFiles[i].Name;

                
if (RemoteFiles[i].Dir )
                
{//是一个目录
                    if ( FileDirectoryName=="." || FileDirectoryName ==".."|| FileDirectoryName=="")
                    
{
                    }

                    
else
                    
{
                        FTPDownloadClient.ChDir (FileDirectoryName);
                        update(local
+@"\"+FileDirectoryName);
                        FTPDownloadClient.CdUp ();
                    }

                }

                
else
                
{//是一个文件
                    if (System.IO .File .Exists (local+@"\"+FileDirectoryName))
                    
{
                        
if (System.IO .File .GetLastWriteTime (local+@"\"+FileDirectoryName) !=    RemoteFiles[i].LastModified)
                        
{
                            FTPDownloadClient.Get (local
+@"\"+FileDirectoryName,FileDirectoryName);
                            System.IO .File .SetLastWriteTime (local
+@"\"+FileDirectoryName,RemoteFiles[i].LastModified);
                            System.IO .File .SetCreationTime (local
+@"\"+FileDirectoryName,RemoteFiles[i].LastModified );
                        }

                    }

                    
else
                    
{
                        FTPDownloadClient.Get (local
+@"\"+FileDirectoryName,FileDirectoryName);
                        System.IO .File .SetLastWriteTime (local
+@"\"+FileDirectoryName,RemoteFiles[i].LastModified);
                        System.IO .File .SetCreationTime (local
+@"\"+FileDirectoryName,RemoteFiles[i].LastModified );
                    }

                }

            }

        }


    }

    
public class PragrammerNew
    
{//检测程序是否最新
        FTPClient FTPDownloadClient;
        
public bool HaveNew(string LocalPath,FTPClient FTPclient)
        
{
            
bool havenew=false;
            FTPDownloadClient
=FTPclient;
            havenew
=update (LocalPath);
            
return havenew;
        }

        
private bool update(string local)
        
{
            
if (!System.IO.Directory .Exists (local)) return true;
            FTPFile[] RemoteFiles;
            
string[] filename=FTPDownloadClient.Dir ();
            RemoteFiles
=FTPDownloadClient.DirDetails("*.*");
            
int filenumbers=RemoteFiles.Length ;
            
string FileDirectoryName=null;
            
for (int i=0;i<filenumbers;i++)
            
{
                FileDirectoryName
=RemoteFiles[i].Name;
                
if (RemoteFiles[i].Dir )
                
{//是一个目录
                    if ( FileDirectoryName=="." || FileDirectoryName ==".."|| FileDirectoryName=="")
                    
{
                    }

                    
else
                    
{
                        FTPDownloadClient.ChDir (FileDirectoryName);
                        
if (update(local+@"\"+FileDirectoryName)) return true;
                        FTPDownloadClient.CdUp ();
                    }

                }

                
else
                
{//是一个文件
                    if (System.IO .File .Exists (local+@"\"+FileDirectoryName))
                    
{
                        
if (System.IO .File .GetLastWriteTime (local+@"\"+FileDirectoryName) !=    RemoteFiles[i].LastModified)
                        
{
                            
return true;
                        }

                    }

                    
else
                    
{
                        
return true;
                    }

                }

            }

            
return false;
        }


    }

}

注意:一、要使用修改过的edtFTPnet组件,支持中文文件名。
二、程序不需要安装,即不打包,COPY后可正确运行(当然也可以下载包)。
三、此组件已在实用中,感觉速度不好。
四、实用中FTP服务器是Serv-u。
五、实用中,使用了两个程序,应用程序(即做事的程序)和升级程序,应用程序使用PragrammerNew检查FTP服务器中的文件与本地文件的最后修改日期是否相同,如全部相同就不需升级,如至少有一个文件不同,则认为需要升级,启动升级程序,并关闭自己;升级程序使用PragrammerUpdate更新应用程序目录下与FTP服务器中最后修改日期不同的文件。
六、如果使用某些FTP客户端软件上传新程序,则FTP服务器中文件最后修改日期为上传时日期,有时可能有些文件没有更新,但PragrammerUpdate仍然会下载更新,可以直接在服务器上挎贝程序则不会出现如些情形。
七、更新程序主要代码:

        public  void 更新()
        
{
            
try
            
{
                FTPClient a
=new FTPClient ();
                a.RemoteHost 
="FTP服务器IP地址";
                a.Connect ();
                a.Login (
"用户名","密码");
           PragrammerUpdate d
= new PragrammerUpdate ();
                d.Update (
@"应用程序目录路径",a);
                a.Quit ();
                
this.label1 .Text="升级完成";
                System.Windows .Forms .MessageBox .Show (
"升级完成");
                
base.Close ();
            }

            
catch (System.Exception er)
            
{
    System.Windows .Forms .MessageBox .Show (er.Message );
            }

        }
posted on 2005-07-27 14:06  爱好  阅读(2305)  评论(0)    收藏  举报