把文件上传到Ftp上面
首先建立配置文件
文件名称App.config:

None.gif<?xml version="1.0" encoding="utf-8" ?>
None.gif<configuration>    
None.gif<appSettings>
None.gif<add key="ConnStr" value="server=SERVER-DBT;database=SZHeritage;uid=szwgj;pwd=wgj;Max Pool Size=20000;"/>
None.gif<add key="FtpServer" value="172.17.101.89"/>
None.gif<add key="FtpUser" value="zaz"/>
None.gif<add key="FtpPwd" value="zaz"/>
None.gif</appSettings>
None.gif</configuration>
None.gif
建立Ftp操作基类 文件名称:FTPClient.cs
None.gifusing System;
None.gifusing System.Net;
None.gifusing System.IO;
None.gifusing System.Text;
None.gifusing System.Net.Sockets;
None.gifusing DevExpress.XtraEditors;
None.gif
None.gifnamespace WindowsApplication6
ExpandedBlockStart.gifContractedBlock.gif运用Ftp进行文件上传(一)_ico_21{
ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
InBlock.gif    /// FTPClient 的摘要说明。
ExpandedSubBlockEnd.gif    /// </summary>
InBlock.gif    public class FTPClient
ExpandedSubBlockStart.gifContractedSubBlock.gif    运用Ftp进行文件上传(一)_ico_21{
ContractedSubBlock.gifExpandedSubBlockStart.gif        构造函数#region 构造函数
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 缺省构造函数
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public FTPClient()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            strRemoteHost  = "";
InBlock.gif            strRemotePath  = "";
InBlock.gif            strRemoteUser  = "";
InBlock.gif            strRemotePass  = "";
InBlock.gif            strRemotePort  = 21;
InBlock.gif            bConnected     = false;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 构造函数
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="remoteHost"></param>
InBlock.gif        /// <param name="remotePath"></param>
InBlock.gif        /// <param name="remoteUser"></param>
InBlock.gif        /// <param name="remotePass"></param>
ExpandedSubBlockEnd.gif        /// <param name="remotePort"></param>
InBlock.gif        public FTPClient( string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort )
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            strRemoteHost  = remoteHost;
InBlock.gif            strRemotePath  = remotePath;
InBlock.gif            strRemoteUser  = remoteUser;
InBlock.gif            strRemotePass  = remotePass;
InBlock.gif            strRemotePort  = remotePort;
InBlock.gif            Connect();
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        登陆#region 登陆
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// FTP服务器IP地址
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private string strRemoteHost;
InBlock.gif        public string RemoteHost
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                return strRemoteHost;
ExpandedSubBlockEnd.gif            }
InBlock.gif            set
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                strRemoteHost = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// FTP服务器端口
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private int strRemotePort;
InBlock.gif        public int RemotePort
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                return strRemotePort;
ExpandedSubBlockEnd.gif            }
InBlock.gif            set
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                strRemotePort = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 当前服务器目录
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private string strRemotePath;
InBlock.gif        public string RemotePath
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                return strRemotePath;
ExpandedSubBlockEnd.gif            }
InBlock.gif            set
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                strRemotePath = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 登录用户账号
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private string strRemoteUser;
InBlock.gif        public string RemoteUser
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            set
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                strRemoteUser = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 用户登录密码
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private string strRemotePass;
InBlock.gif        public string RemotePass
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            set
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                strRemotePass = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 是否登录
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private Boolean bConnected;
InBlock.gif        public bool Connected
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                return bConnected;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        链接#region 链接
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 建立连接 
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public void Connect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            socketControl = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
InBlock.gif            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort);
InBlock.gif            // 链接
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                socketControl.Connect(ep);
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException("Couldn't connect to remote server");
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            // 获取应答码
InBlock.gif            ReadReply();
InBlock.gif            if(iReplyCode != 220)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                DisConnect();
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            // 登陆
InBlock.gif            SendCommand("USER "+strRemoteUser);
InBlock.gif            if( !(iReplyCode == 331 || iReplyCode == 230) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                CloseSocketConnect();//关闭连接
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            if( iReplyCode != 230 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                SendCommand("PASS "+strRemotePass);
InBlock.gif                if( !(iReplyCode == 230 || iReplyCode == 202) )
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    CloseSocketConnect();//关闭连接
InBlock.gif                    throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            bConnected = true;
InBlock.gif
InBlock.gif            // 切换到目录
InBlock.gif            ChDir(strRemotePath);
ExpandedSubBlockEnd.gif        }
InBlock.gif    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 关闭连接
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public void DisConnect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if( socketControl != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                SendCommand("QUIT");
ExpandedSubBlockEnd.gif            }
InBlock.gif            CloseSocketConnect();
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        传输模式#region 传输模式
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 传输模式:二进制类型、ASCII类型
ExpandedSubBlockEnd.gif        /// </summary>
ExpandedSubBlockStart.gifContractedSubBlock.gif        public enum TransferType 运用Ftp进行文件上传(一)_ico_21{Binary,ASCII};
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 设置传输模式
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="ttType">传输模式</param>
InBlock.gif        public void SetTransferType(TransferType ttType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(ttType == TransferType.Binary)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                SendCommand("TYPE I");//binary类型传输
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                SendCommand("TYPE A");//ASCII类型传输
ExpandedSubBlockEnd.gif            }
InBlock.gif            if (iReplyCode != 200)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                trType = ttType;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 获得传输模式
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <returns>传输模式</returns>
InBlock.gif        public TransferType GetTransferType()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            return trType;
ExpandedSubBlockEnd.gif        }
InBlock.gif    
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        文件操作#region 文件操作
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 获得文件列表
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="strMask">文件名的匹配字符串</param>
ExpandedSubBlockEnd.gif        /// <returns></returns>
InBlock.gif        public string[] Dir(string strMask)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            // 建立链接
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            //建立进行数据连接的socket
InBlock.gif            Socket socketData = CreateDataSocket();
InBlock.gif   
InBlock.gif            //传送命令
InBlock.gif            SendCommand("NLST " + strMask);
InBlock.gif
InBlock.gif            //分析应答代码
InBlock.gif            if(!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            //获得结果
InBlock.gif            strMsg = "";
InBlock.gif            while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                int iBytes = socketData.Receive(buffer, buffer.Length, 0);
InBlock.gif                strMsg += ASCII.GetString(buffer, 0, iBytes);
InBlock.gif                if(iBytes < buffer.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    break;
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockStart.gifContractedSubBlock.gif            char[] seperator = 运用Ftp进行文件上传(一)_ico_21{'\n'};
InBlock.gif            string[] strsFileList = strMsg.Split(seperator);
InBlock.gif            socketData.Close();//数据socket关闭时也会有返回码
InBlock.gif            if(iReplyCode != 226)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ReadReply();
InBlock.gif                if(iReplyCode != 226)
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            return strsFileList;
ExpandedSubBlockEnd.gif        }
InBlock.gif    
InBlock.gif
InBlock.gif        public void newPutByGuid(string strFileName,string strGuid)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif
InBlock.gif            if(!bConnected)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif
InBlock.gif                Connect();
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            string str = strFileName.Substring(0,strFileName.LastIndexOf("\\"));
InBlock.gif
InBlock.gif            string strTypeName = strFileName.Substring(strFileName.LastIndexOf("."));
InBlock.gif
InBlock.gif            strGuid = str + "\\" + strGuid ;
InBlock.gif
InBlock.gif            //System.IO.File.Copy(strFileName,strGuid); 
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            Socket socketData = CreateDataSocket();
InBlock.gif
InBlock.gif            SendCommand("STOR "+Path.GetFileName(strGuid));
InBlock.gif
InBlock.gif            if( !(iReplyCode == 125 || iReplyCode == 150) )
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif
InBlock.gif                throw new IOException(strReply.Substring(4));
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif           
InBlock.gif
InBlock.gif           
InBlock.gif
InBlock.gif            FileStream input = new 
InBlock.gif
InBlock.gif                FileStream(strGuid,FileMode.Open);
InBlock.gif
InBlock.gif            input.Flush();
InBlock.gif
InBlock.gif           
InBlock.gif
InBlock.gif           
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            int iBytes = 0;
InBlock.gif
InBlock.gif            while ((iBytes = input.Read(buffer,0,buffer.Length)) > 0)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif
InBlock.gif                socketData.Send(buffer, iBytes, 0);
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            input.Close();
InBlock.gif
InBlock.gif            //File.Delete(strGuid);
InBlock.gif
InBlock.gif            if (socketData.Connected)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif
InBlock.gif                socketData.Close();
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            if(!(iReplyCode == 226 || iReplyCode == 250))
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif
InBlock.gif                ReadReply();
InBlock.gif
InBlock.gif                if(!(iReplyCode == 226 || iReplyCode == 250))
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif
InBlock.gif                    throw new IOException(strReply.Substring(4));
InBlock.gif
ExpandedSubBlockEnd.gif                }
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 获取文件大小
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="strFileName">文件名</param>
ExpandedSubBlockEnd.gif        /// <returns>文件大小</returns>
InBlock.gif        private long GetFileSize(string strFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            SendCommand("SIZE " + Path.GetFileName(strFileName));
InBlock.gif            long lSize=0;
InBlock.gif            if(iReplyCode == 213)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                lSize = Int64.Parse(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            return lSize;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 删除
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="strFileName">待删除文件名</param>
InBlock.gif        public void Delete(string strFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            SendCommand("DELE "+strFileName);
InBlock.gif            if(iReplyCode != 250)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 重命名(如果新文件名与已有文件重名,将覆盖已有文件)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="strOldFileName">旧文件名</param>
ExpandedSubBlockEnd.gif        /// <param name="strNewFileName">新文件名</param>
InBlock.gif        public void Rename(string strOldFileName,string strNewFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            SendCommand("RNFR "+strOldFileName);
InBlock.gif            if(iReplyCode != 350)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            //  如果新文件名与原有文件重名,将覆盖原有文件
InBlock.gif            SendCommand("RNTO "+strNewFileName);
InBlock.gif            if(iReplyCode != 250)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        上传和下载#region 上传和下载
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 下载一批文件
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="strFileNameMask">文件名的匹配字符串</param>
ExpandedSubBlockEnd.gif        /// <param name="strFolder">本地目录(不得以\结束)</param>
InBlock.gif        public void Get(string strFileNameMask,string strFolder)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            string[] strFiles = Dir(strFileNameMask);
InBlock.gif            foreach(string strFile in strFiles)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                if(!strFile.Equals(""))//一般来说strFiles的最后一个元素可能是空字符串
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    Get(strFile,strFolder,strFile);
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif    
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 下载一个文件
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="strRemoteFileName">要下载的文件名</param>
InBlock.gif        /// <param name="strFolder">本地目录(不得以\结束)</param>
ExpandedSubBlockEnd.gif        /// <param name="strLocalFileName">保存在本地时的文件名</param>
InBlock.gif        public void Get(string strRemoteFileName,string strFolder,string strLocalFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            SetTransferType(TransferType.Binary);
InBlock.gif            if (strLocalFileName.Equals(""))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                strLocalFileName = strRemoteFileName;
ExpandedSubBlockEnd.gif            }
InBlock.gif            //            if(!File.Exists(strFolder + "\\" + strLocalFileName))
InBlock.gif            //            {
InBlock.gif            //                Stream st = File.Create(strFolder + "\\" + strLocalFileName);
InBlock.gif            //                st.Close();
InBlock.gif            //            }
InBlock.gif            FileStream output = new 
InBlock.gif                FileStream(strFolder + "\\" + strLocalFileName,FileMode.Create);
InBlock.gif            Socket socketData = CreateDataSocket();
InBlock.gif            SendCommand("RETR " + strRemoteFileName);
InBlock.gif            if(!(iReplyCode == 150 || iReplyCode == 125
InBlock.gif                || iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                int iBytes = socketData.Receive(buffer, buffer.Length, 0);
InBlock.gif                output.Write(buffer,0,iBytes);
InBlock.gif                if(iBytes <= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    break;
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            output.Close();
InBlock.gif            if (socketData.Connected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                socketData.Close();
ExpandedSubBlockEnd.gif            }
InBlock.gif            if(!(iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ReadReply();
InBlock.gif                if(!(iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 下载一个文件
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="strRemoteFileName">要下载的文件名</param>
InBlock.gif        /// <param name="strFolder">本地目录(不得以\结束)</param>
ExpandedSubBlockEnd.gif        /// <param name="strLocalFileName">保存在本地时的文件名</param>
InBlock.gif        public void Get(string strRemoteFileName,string strFolder,string strLocalFileName,ProgressBarControl p1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            SetTransferType(TransferType.Binary);
InBlock.gif            if (strLocalFileName.Equals(""))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                strLocalFileName = strRemoteFileName;
ExpandedSubBlockEnd.gif            }
InBlock.gif            //            if(!File.Exists(strFolder + "\\" + strLocalFileName))
InBlock.gif            //            {
InBlock.gif            //                Stream st = File.Create(strFolder + "\\" + strLocalFileName);
InBlock.gif            //                st.Close();
InBlock.gif            //            }
InBlock.gif            FileStream output = new 
InBlock.gif                FileStream(strFolder + "\\" + strLocalFileName,FileMode.Create);
InBlock.gif            Socket socketData = CreateDataSocket();
InBlock.gif            SendCommand("RETR " + strRemoteFileName);
InBlock.gif            if(!(iReplyCode == 150 || iReplyCode == 125
InBlock.gif                || iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            //add by jy
InBlock.gif            int iBytes = 0;
InBlock.gif            int FileSize = Convert.ToInt32(output.Length)/256000;
InBlock.gif            p1.Properties.Maximum = FileSize;
InBlock.gif            int count = 0;
InBlock.gif            while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                while((iBytes = socketData.Receive(buffer, buffer.Length, 0))>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    count += 1;
InBlock.gif                    if(count == 500)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                        p1.Position +=1;
InBlock.gif                        count = 0;
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    output.Write(buffer,0,iBytes);
ExpandedSubBlockEnd.gif                }
InBlock.gif                if(iBytes <= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    break;
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            output.Close();
InBlock.gif            if(p1.Position  != FileSize)
InBlock.gif                p1.Position += 1;
InBlock.gif
InBlock.gif            if (socketData.Connected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                socketData.Close();
ExpandedSubBlockEnd.gif            }
InBlock.gif            if(!(iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ReadReply();
InBlock.gif                if(!(iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ContractedSubBlock.gifExpandedSubBlockStart.gif        不带进度条参数的上传方法#region 不带进度条参数的上传方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 上传一批文件
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="strFolder">本地目录(不得以\结束)</param>
ExpandedSubBlockEnd.gif        /// <param name="strFileNameMask">文件名匹配字符(可以包含*和?)</param>
InBlock.gif        public void Put(string strFolder,string strFileNameMask)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            string[] strFiles = Directory.GetFiles(strFolder,strFileNameMask);
InBlock.gif            foreach(string strFile in strFiles)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                //strFile是完整的文件名(包含路径)
InBlock.gif                Put(strFile);
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 上传一个文件
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="strFileName">本地文件名</param>
InBlock.gif        public void Put(string strFileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            Socket socketData = CreateDataSocket();
InBlock.gif            SendCommand("STOR "+Path.GetFileName(strFileName));
InBlock.gif            if( !(iReplyCode == 125 || iReplyCode == 150) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            
InBlock.gif            FileStream input = new 
InBlock.gif                FileStream(strFileName,FileMode.Open);
InBlock.gif            int iBytes = 0;
InBlock.gif            while ((iBytes = input.Read(buffer,0,buffer.Length)) > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                socketData.Send(buffer, iBytes, 0);
ExpandedSubBlockEnd.gif            }
InBlock.gif            input.Close();
InBlock.gif            if (socketData.Connected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                socketData.Close();
ExpandedSubBlockEnd.gif            }
InBlock.gif            if(!(iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ReadReply();
InBlock.gif                if(!(iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockEnd.gif        #endregion
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 上传一个文件
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="strFileName">本地文件名</param>
InBlock.gif        public void PutByGuid(string strFileName,string strGuid,ProgressBarControl p1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            string str = strFileName.Substring(0,strFileName.LastIndexOf("\\"));
InBlock.gif            string strTypeName = strFileName.Substring(strFileName.LastIndexOf("."));
InBlock.gif            strGuid = str + "\\" + strGuid ;
InBlock.gif            
InBlock.gif            System.IO.File.Move(strFileName,strGuid);
InBlock.gif
InBlock.gif            //add by wzy
InBlock.gif            System.IO.File.SetAttributes(strGuid,System.IO.FileAttributes.Normal);
InBlock.gif            Socket socketData = CreateDataSocket();
InBlock.gif            SendCommand("STOR "+Path.GetFileName(strGuid));
InBlock.gif            if( !(iReplyCode == 125 || iReplyCode == 150) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            //change by wzy
InBlock.gif            FileStream input = new FileStream(strGuid,FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read);
InBlock.gif            
InBlock.gif            int iBytes = 0;
InBlock.gif            int FileSize = Convert.ToInt32(input.Length)/256000;
InBlock.gif            p1.Properties.Maximum = FileSize;
InBlock.gif            int count = 0;
InBlock.gif            while ((iBytes = input.Read(buffer,0,buffer.Length)) > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                count += 1;
InBlock.gif                if(count == 500)
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    p1.Position +=1;
InBlock.gif                    count = 0;
ExpandedSubBlockEnd.gif                }
InBlock.gif                socketData.Send(buffer, iBytes, 0);
ExpandedSubBlockEnd.gif            }
InBlock.gif            input.Close();
InBlock.gif            if(p1.Position  != FileSize)
InBlock.gif                p1.Position += 1;
InBlock.gif            
InBlock.gif            System.IO.File.Move(strGuid,strFileName);
InBlock.gif            if (socketData.Connected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                socketData.Close();
ExpandedSubBlockEnd.gif            }
InBlock.gif            if(!(iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ReadReply();
InBlock.gif                if(!(iReplyCode == 226 || iReplyCode == 250))
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        目录操作#region 目录操作
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 创建目录
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="strDirName">目录名</param>
InBlock.gif        public void MkDir(string strDirName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            SendCommand("MKD "+strDirName);
InBlock.gif            if(iReplyCode != 257)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif    
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 删除目录
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="strDirName">目录名</param>
InBlock.gif        public void RmDir(string strDirName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            SendCommand("RMD "+strDirName);
InBlock.gif            if(iReplyCode != 250)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif    
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 改变目录
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="strDirName">新的工作目录名</param>
InBlock.gif        public void ChDir(string strDirName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(strDirName.Equals(".") || strDirName.Equals(""))
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                return;
ExpandedSubBlockEnd.gif            }
InBlock.gif            if(!bConnected)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Connect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            SendCommand("CWD "+strDirName);
InBlock.gif            if(iReplyCode != 250)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            this.strRemotePath = strDirName;
ExpandedSubBlockEnd.gif        }
InBlock.gif    
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        内部变量#region 内部变量
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 服务器返回的应答信息(包含应答码)
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private string strMsg;
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 服务器返回的应答信息(包含应答码)
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private string strReply;
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 服务器返回的应答码
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private int iReplyCode;
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 进行控制连接的socket
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private Socket socketControl;
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 传输模式
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private TransferType trType;
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 接收和发送数据的缓冲区
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private static int BLOCK_SIZE = 512;
InBlock.gif        Byte[] buffer = new Byte[BLOCK_SIZE];
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 编码方式
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        Encoding ASCII = Encoding.ASCII;
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        内部函数#region 内部函数
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 将一行应答字符串记录在strReply和strMsg
InBlock.gif        /// 应答码记录在iReplyCode
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private void ReadReply()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            strMsg = "";
InBlock.gif            strReply = ReadLine();
InBlock.gif            iReplyCode = Int32.Parse(strReply.Substring(0,3));
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 建立进行数据连接的socket
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <returns>数据连接socket</returns>
InBlock.gif        private Socket CreateDataSocket()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            SendCommand("PASV");
InBlock.gif            if(iReplyCode != 227)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException(strReply.Substring(4));
ExpandedSubBlockEnd.gif            }
InBlock.gif            int index1 = strReply.IndexOf('(');
InBlock.gif            int index2 = strReply.IndexOf(')');
InBlock.gif            string ipData = 
InBlock.gif                strReply.Substring(index1+1,index2-index1-1);
InBlock.gif            int[] parts = new int[6];
InBlock.gif            int len = ipData.Length;
InBlock.gif            int partCount = 0;
InBlock.gif            string buf="";
InBlock.gif            for (int i = 0; i < len && partCount <= 6; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                char ch = Char.Parse(ipData.Substring(i,1));
InBlock.gif                if (Char.IsDigit(ch))
InBlock.gif                    buf+=ch;
InBlock.gif                else if (ch != ',')
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    throw new IOException("Malformed PASV strReply: " + 
InBlock.gif                        strReply);
ExpandedSubBlockEnd.gif                }
InBlock.gif                if (ch == ',' || i+1 == len)
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                        parts[partCount++] = Int32.Parse(buf);
InBlock.gif                        buf="";
ExpandedSubBlockEnd.gif                    }
InBlock.gif                    catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                        throw new IOException("Malformed PASV strReply: " + 
InBlock.gif                            strReply);
ExpandedSubBlockEnd.gif                    }
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            string ipAddress = parts[0] + "."+ parts[1]+ "." +
InBlock.gif                parts[2] + "." + parts[3];
InBlock.gif            int port = (parts[4] << 8) + parts[5];
InBlock.gif            Socket s = new 
InBlock.gif                Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
InBlock.gif            IPEndPoint ep = new 
InBlock.gif                IPEndPoint(IPAddress.Parse(ipAddress), port);
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                s.Connect(ep);
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch(Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                throw new IOException("Can't connect to remote server");
ExpandedSubBlockEnd.gif            }
InBlock.gif            return s;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 关闭socket连接(用于登录以前)
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        private void CloseSocketConnect()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            if(socketControl!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                socketControl.Close();
InBlock.gif                socketControl = null;
ExpandedSubBlockEnd.gif            }
InBlock.gif            bConnected = false;
ExpandedSubBlockEnd.gif        }
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 读取Socket返回的所有字符串
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <returns>包含应答码的字符串行</returns>
InBlock.gif        private string ReadLine()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            while(true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                int iBytes = socketControl.Receive(buffer, buffer.Length, 0);
InBlock.gif                strMsg += ASCII.GetString(buffer, 0, iBytes);
InBlock.gif                if(iBytes < buffer.Length)
ExpandedSubBlockStart.gifContractedSubBlock.gif                运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                    break;
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockStart.gifContractedSubBlock.gif            char[] seperator = 运用Ftp进行文件上传(一)_ico_21{'\n'};
InBlock.gif            string[] mess = strMsg.Split(seperator);
InBlock.gif            if(strMsg.Length > 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                strMsg = mess[mess.Length-2];
InBlock.gif                //seperator[0]是10,换行符是由13和0组成的,分隔后10后面虽没有字符串,
InBlock.gif                //但也会分配为空字符串给后面(也是最后一个)字符串数组,
InBlock.gif                //所以最后一个mess是没用的空字符串
InBlock.gif                //但为什么不直接取mess[0],因为只有最后一行字符串应答码与信息之间有空格
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                strMsg = mess[0];
ExpandedSubBlockEnd.gif            }
InBlock.gif            if(!strMsg.Substring(3,1).Equals(" "))//返回字符串正确的是以应答码(如220开头,后面接一空格,再接问候字符串)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                return ReadLine();
ExpandedSubBlockEnd.gif            }
InBlock.gif            return strMsg;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 发送命令并获取应答码和最后一行应答字符串
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="strCommand">命令</param>
InBlock.gif        private void SendCommand(String strCommand)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                Byte[] cmdBytes = 
InBlock.gif                    Encoding.ASCII.GetBytes((strCommand+"\r\n").ToCharArray());
InBlock.gif                socketControl.Send(cmdBytes, cmdBytes.Length, 0);
InBlock.gif                ReadReply();
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif            
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif

建立附件操作基类FileOperator.cs
None.gifusing System;
None.gifusing System.Drawing;
None.gifusing System.Collections;
None.gifusing System.ComponentModel;
None.gifusing System.Windows.Forms;
None.gifusing System.Data;
None.gifusing DevExpress.XtraEditors;
None.gifusing System.IO;
None.gif
None.gif
None.gifnamespace WindowsApplication6
ExpandedBlockStart.gifContractedBlock.gif运用Ftp进行文件上传(一)_ico_21{
ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
InBlock.gif    /// 文件操作集合类
InBlock.gif    /// 为其他模块提供文件操作方法
ExpandedSubBlockEnd.gif    /// </summary>
InBlock.gif    public class FileOperator
ExpandedSubBlockStart.gifContractedSubBlock.gif    运用Ftp进行文件上传(一)_ico_21{
InBlock.gif        public FileOperator()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            //
InBlock.gif            // TODO: 在此处添加构造函数逻辑
InBlock.gif            //
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        
InBlock.gif        private bool b_sign;
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 文件集合操作标志 true 操作成功 false 操作失败
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public bool b_OperatorSign
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                return b_sign;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
ContractedSubBlock.gifExpandedSubBlockStart.gif        附件操作方法集合#region 附件操作方法集合
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 写附件至服务器
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="fileName">原文件路径("c:\tt\a.txt")</param>
ExpandedSubBlockEnd.gif        /// <param name="guidname">服务器文件名("asdf-wer-46wer.doc")</param>
InBlock.gif        public bool writeAttach(string fileName,string guidname,ProgressBarControl p1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            FTPClient ftp = new FTPClient();
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ftp = this.getFtpClient();
InBlock.gif                ftp.PutByGuid(fileName,guidname,p1);
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch(Exception ft)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                string s = ft.Message;
InBlock.gif                ftp.DisConnect();
InBlock.gif                XtraMessageBox.Show("传输失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
InBlock.gif                return false;
ExpandedSubBlockEnd.gif            }
InBlock.gif            ftp.DisConnect();
InBlock.gif            return true;
ExpandedSubBlockEnd.gif        }
InBlock.gif        public bool newwriteAttach(string fileName,string guidname)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            FTPClient ftp = new FTPClient();
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                
InBlock.gif                ftp = this.getFtpClient();
InBlock.gif                ftp.newPutByGuid(fileName,guidname);
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch(Exception ft)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ftp.DisConnect();
InBlock.gif                XtraMessageBox.Show("传输失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
InBlock.gif                return false;
ExpandedSubBlockEnd.gif            }
InBlock.gif            ftp.DisConnect();
InBlock.gif            return true;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 删除附件
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="fileName">删除服务器的文件名("a.txt")</param>
InBlock.gif    
InBlock.gif        public bool delAttach(string fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            FTPClient ftp = new FTPClient();
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                
InBlock.gif                ftp = this.getFtpClient();
InBlock.gif                ftp.Delete(fileName);
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch(Exception ft)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                XtraMessageBox.Show("删除失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
InBlock.gif                ftp.DisConnect();
InBlock.gif                return false;
ExpandedSubBlockEnd.gif            }
InBlock.gif            ftp.DisConnect();
InBlock.gif            return true;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 打开附件
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="fileName"></param>
InBlock.gif        public void openAttach(string fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            
InBlock.gif            string path =Path.GetTempPath();
InBlock.gif            FTPClient ftp = new FTPClient();
InBlock.gif            if(!System.IO.Directory.Exists(path))
InBlock.gif                System.IO.Directory.CreateDirectory(path);
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                
InBlock.gif                ftp = this.getFtpClient();
InBlock.gif                ftp.Get(fileName,path,fileName);
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch(Exception ft)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                string s = ft.Message;
InBlock.gif                XtraMessageBox.Show("网络传输错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
InBlock.gif                ftp.DisConnect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            System.Diagnostics.Process p =  new System.Diagnostics.Process();
InBlock.gif            p.StartInfo.FileName = path + "\\" + fileName;
InBlock.gif            p.StartInfo.UseShellExecute = true;
InBlock.gif
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                p.Start();
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                XtraMessageBox.Show("无法打开文件,系统无关联后缀名 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
InBlock.gif
ExpandedSubBlockEnd.gif            }
InBlock.gif            ftp.DisConnect();
InBlock.gif
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 下载附件
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <param name="fileName"></param>
InBlock.gif        public void getAttach(string fileName,string path,ProgressBarControl p1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            FTPClient ftp = new FTPClient();
InBlock.gif            if(!System.IO.Directory.Exists(path))
InBlock.gif                System.IO.Directory.CreateDirectory(path);
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ftp = this.getFtpClient();
InBlock.gif                //fileName:要下载的文件名 path:本地目录 fileName:保存在本地时的文件名 p1 滚动条
InBlock.gif                ftp.Get(fileName,path,fileName,p1);
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch(Exception ft)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                string s = ft.Message;
InBlock.gif                XtraMessageBox.Show("网络传输错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
InBlock.gif                ftp.DisConnect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            ftp.DisConnect();
InBlock.gif
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 下载附件
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="ftpfileName">要下载的文件名</param>
InBlock.gif        /// <param name="path">本地目录</param>
InBlock.gif        /// <param name="localfileName">保存在本地时的文件名</param>
ExpandedSubBlockEnd.gif        /// <param name="p1">滚动条</param>
InBlock.gif        public void getAttach(string ftpfileName,string path,string localfileName,ProgressBarControl p1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            FTPClient ftp = new FTPClient();
InBlock.gif            if(!System.IO.Directory.Exists(path))
InBlock.gif                System.IO.Directory.CreateDirectory(path);
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ftp = this.getFtpClient();
InBlock.gif                //fileName:要下载的文件名 path:本地目录 localfileName:保存在本地时的文件名 p1 滚动条
InBlock.gif                ftp.Get(ftpfileName,path,localfileName,p1);
InBlock.gif                b_sign=true;
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch(Exception ft)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                string s = ft.Message;
InBlock.gif                XtraMessageBox.Show("网络传输错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
InBlock.gif                ftp.DisConnect();
InBlock.gif                b_sign=false;
ExpandedSubBlockEnd.gif            }
InBlock.gif            ftp.DisConnect();
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        public void newgetAttach(string fileName,string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            FTPClient ftp = new FTPClient();
InBlock.gif            if(!System.IO.Directory.Exists(path))
InBlock.gif                System.IO.Directory.CreateDirectory(path);
InBlock.gif            try
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                ftp = this.getFtpClient();
InBlock.gif                ftp.Get(fileName,path,fileName);
ExpandedSubBlockEnd.gif            }
InBlock.gif            catch(Exception ft)
ExpandedSubBlockStart.gifContractedSubBlock.gif            运用Ftp进行文件上传(一)_ico_21{
InBlock.gif                string s = ft.Message;
InBlock.gif                XtraMessageBox.Show("网络传输错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
InBlock.gif                ftp.DisConnect();
ExpandedSubBlockEnd.gif            }
InBlock.gif            ftp.DisConnect();
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        ///得到ftp传输对象
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public FTPClient  getFtpClient()
ExpandedSubBlockStart.gifContractedSubBlock.gif        运用Ftp进行文件上传(一)_ico_21{
InBlock.gif            string strHost = System.Configuration.ConfigurationSettings.AppSettings["FtpServer"].ToString();
InBlock.gif            string strUser = System.Configuration.ConfigurationSettings.AppSettings["FtpUser"].ToString();
InBlock.gif            string strPwd = System.Configuration.ConfigurationSettings.AppSettings["FtpPwd"].ToString();
InBlock.gif            
InBlock.gif            FTPClient ft = new FTPClient();
InBlock.gif            ft.RemoteHost = strHost;
InBlock.gif            ft.RemotePort = 21;
InBlock.gif            ft.RemoteUser = strUser;
InBlock.gif            ft.RemotePass = strPwd;
InBlock.gif            
InBlock.gif            return ft ;
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif        #endregion
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}
None.gif