關(guān)于c#連接ftp進(jìn)行上傳下載實現(xiàn)原理及代碼
更新時間:2013年01月24日 17:12:21 作者:
ftp上傳下載想必大家已經(jīng)很熟悉了,關(guān)于c#連接ftp進(jìn)行上傳下載,一些新手朋友應(yīng)該會很陌生吧,本文將帶你解決困惑,感興趣的朋友可以了解下哦,就當(dāng)鞏固知識了
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace ftponload
{
class Program
{
static void Main(string[] args)
{
//上傳文件的方法
onload("D://outPut.txt");
//下載文件的方法
fload();
}
public static void onload(string file)
{
//構(gòu)造一個web服務(wù)器的請求對象
FtpWebRequest ftp;
//實例化一個文件對象
FileInfo f = new FileInfo(file);
ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://192.168.0.150/" + f.Name));
//創(chuàng)建用戶名和密碼
ftp.Credentials = new NetworkCredential("123", "123");
ftp.KeepAlive = false;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
ftp.UseBinary = true;
ftp.ContentLength = f.Length;
int buffLength = 20480;
byte[] buff = new byte[buffLength];
int contentLen;
try
{
//獲得請求對象的輸入流
FileStream fs = f.OpenRead();
Stream sw = ftp.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
sw.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
sw.Close();
fs.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public static void fload()
{
FtpWebRequest ftp;
ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://192.168.0.6/連接到你指定的文件"));
//指定用戶名和密碼
ftp.Credentials = new NetworkCredential("123", "123456");
WebResponse wr = ftp.GetResponse();
StreamReader sr = new StreamReader(wr.GetResponseStream(),System.Text.Encoding.Default);
string s = sr.ReadLine();
while(s.Equals(""))
{
s = sr.ReadLine();
}
}
}
}
相關(guān)文章
Asp.Mvc?2.0實現(xiàn)用戶注冊實例講解(1)
這篇文章主要介紹了Asp.Mvc?2.0如何實現(xiàn)用戶注冊,實例講解很細(xì)致,注冊功能是每個網(wǎng)站必不可少的組成部分,感興趣的的朋友可以參考下2015-08-08asp.net 使用Silverlight操作ASPNETDB數(shù)據(jù)庫
asp.net下使用Silverlight操作ASPNETDB數(shù)據(jù)庫的實現(xiàn)代碼2010-01-01ASP.NET 2.0 中收集的小功能點(轉(zhuǎn))
ASP.NET 2.0 中收集的小功能點(轉(zhuǎn))...2006-12-12