当前位置: 移动技术网 > IT编程>开发语言>.net > 关于c#连接ftp进行上传下载实现原理及代码

关于c#连接ftp进行上传下载实现原理及代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

圣门风云,黑豚鼠养殖,今日铜价格走势图

复制代码 代码如下:

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)
{
//构造一个web服务器的请求对象
ftpwebrequest ftp;
//实例化一个文件对象
fileinfo f = new fileinfo(file);
ftp = (ftpwebrequest)ftpwebrequest.create(new uri("ftp://192.168.0.150/" + f.name));
//创建用户名和密码
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();
}
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网