日期:2014-05-17  浏览次数:20923 次

C#实现FTP方式文件上传
 
class SoftUpdate
    {
       //文件上传
        public static void FileUpLoad()
        {
            try
            {
                XmlParse serverXmlParse = new XmlParse(Application.StartupPath + @"\UpdateList.xml");
                string url = serverXmlParse.GetNodeValue("AutoUpdater/Updater/Url").ToString().Trim();
              //  WebClient server = new WebClient();
                try
                {
                    
                    //上传文件
                    XmlNode node = serverXmlParse.getNode("AutoUpdater/Updater/LastUpdateTime");
                    //修改xml时间结点的值
                    node.InnerText = DateTime.Now.Date.ToString("yyyy-MM-dd").ToString().Trim();
                 
                    FtpWebRequest reqFTP=null;
                    Stream stream =null;
                    //待上传的文件 (全路径)
                    string xmlPath = Application.StartupPath + @"\UpdateList.xml";
                    string databaseFilePath = Application.StartupPath + @"\ExamSystem.mdf";
                    //获取连接FTP的账户
                    string ftpAccount = serverXmlParse.GetNodeValue("AutoUpdater/ftp/account").ToString().Trim();
                    //获取连接FTP的密码
                    string ftpPassword = serverXmlParse.GetNodeValue("AutoUpdater/ftp/password").ToString().Trim();
                    try
                    {
                        FileInfo fileInfo = new FileInfo(xmlPath);
                        FileStream fs = fileInfo.OpenRead();
                        long length = fs.Length;   
                        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url+fileInfo.Name));

                        //设置连接到FTP的帐号密码
                        reqFTP.Credentials = new NetworkCredential(ftpAccount, ftpPassword);
                       //设置请求完成后是否保持连接
                        reqFTP.KeepAlive = false;
                        //指定执行命令
                        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                        //指定数据传输类型
                        reqFTP.UseBinary = true;

                          stream = reqFTP.GetRequestStream();
                        //设置缓冲大小
                        int BufferLength = 5120;
                        byte[] b = new byte[BufferLength];
                        int i;
                        while ((i = fs.Read(b, 0, BufferLength)) > 0)
                        {
                            stream.Write(b, 0, i);
                        }
                        stream.Close();
                        stream.Dispose();   
                        MessageBox.Show("文件上传成功", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("上传文件失败错误为" + ex.Message, "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        stream.Close();
                        stream.Dispose();
                    }
                    finally
                    {
                          
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("上传文件失败错误为"+ex.Message, "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                finally
                {
                  
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("上传文件失败错误为"+ex.Message, "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        
    }