复制代码 代码如下:

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();

}

}

}

}
您可能感兴趣的文章:

  • c# FTP上传文件实例代码(简易版)
  • C#利用SFTP实现上传下载
  • C# 中实现ftp 图片上传功能(多快好省)
  • C#开发教程之FTP上传下载功能详解
  • C# 实现FTP客户端的小例子
  • C#开发windows服务实现自动从FTP服务器下载文件
  • C#基于FTP协议的简易软件自动升级程序
  • C#实现FTP客户端的案例
  • FtpHelper实现ftp服务器文件读写操作(C#)
  • C#操作ftp类完整实例
  • C# 实现FTP上传资料的示例
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。