c# 上傳FTP檔案

iDotNetSpace發表於2009-07-14
/// 
        
/// 向FTP上傳檔案
        
/// 
        
/// 賬戶
        
/// 密碼
        
/// 地址
        
/// XML物件
        private static void UpLoadFile(string FtpName, string FtpPassword, Uri FtpUrl, XElement root)
        {

            FtpWebRequest request 
= (FtpWebRequest)WebRequest.Create(FtpUrl);
            request.Credentials 
= new NetworkCredential(FtpName, FtpPassword);
            request.KeepAlive 
= false;
            request.Method 
= WebRequestMethods.Ftp.UploadFile;

            
//建立一條記憶體流
            MemoryStream fileStream = new MemoryStream();
            StreamWriter sw 
= new StreamWriter(fileStream);
            root.Save(sw);
            sw.Write(fileStream);

            
try
            {
                var buffer 
= new byte[20480];
                
int conlen = fileStream.Read(buffer, 020480);
                Stream stream 
= request.GetRequestStream();
                
foreach (var item in fileStream.ToArray())
                {
                    stream.WriteByte(item);
                }
                stream.Close();
                fileStream.Close();
            }
            
catch (Exception e)
            {
                
throw;
            }

        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-609076/,如需轉載,請註明出處,否則將追究法律責任。

相關文章