using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace FTP_test2
{
public partial class Form1 : Form
{
FtpWebRequest request;
FtpWebResponse response;
string fileName = "";
StreamReader reader;
public Form1()
{
InitializeComponent();
}
//连接按钮
private void btn_connect_Click(object sender, EventArgs e)
{
//先清空列表框
listBox1.Items.Clear();
//对FTP的操作,使用FtpWebRequest类和FtpWebResponse类
request = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.98");
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("user", "user");
response = (FtpWebResponse)request.GetResponse();
//再使用一个StreamReader类
reader = new StreamReader(response.GetResponseStream());
string filelist = reader.ReadToEnd();
string[] list = filelist.Split(new char[]{'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
foreach(string i in list)
{
listBox1.Items.Add(i);
}
reader.Close();
response.Close();
}
//下载按钮
private void btn_down_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("please chose a file!");
return;
}
fileName = (string)listBox1.SelectedItem.ToString();
string url = "D://Client//" + fileName;
if(File.Exists(url))
{
if (MessageBox.Show("The location is already exit the" + fileName + ",Click OK will be covered!", "tooltip",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
{
return;
}
}
request = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.98/" + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("user", "user");
response = (FtpWebResponse)request.GetResponse();
byte[] bt = null;
bt = DownLoadFile();
if (bt != null)
{
FileStream stream = new FileStream(url,FileMode.Create);
stream.Write(bt, 0, bt.Length);
stream.Flush();
stream.Close();
}
MessageBox.Show(fileName + "download completed!");
/*
* //非二进制传输方式,文本下载后可以打开,但视频图片使用这种方式下载都会损坏
*
request = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.98/" + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("user", "user");
response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
reader = new StreamReader(responseStream);
StreamWriter writer = new StreamWriter(url);
writer.Write(reader.ReadToEnd());
writer.Close();
reader.Close();
MessageBox.Show(fileName + "download completed!");
*/
}
//二进制传输文件方式
public byte[] DownLoadFile()
{
try
{
Stream responseStream = response.GetResponseStream();
//将流内容写入字节数组
MemoryStream men = new MemoryStream(1024 * 500);
byte[] buffer = new byte[1024];
int bytesRead = 0;
int TotalByteRead = 0;
while (true)
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
TotalByteRead += bytesRead;
if (bytesRead == 0)
break;
men.Write(buffer, 0, bytesRead);
}
if (men.Length > 0)
{
return men.ToArray();
}
else
{
return null;
}
}
catch(Exception ep)
{
throw ep;
}
}
}
}
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
namespace FTP_test2
{
public partial class Form1 : Form
{
FtpWebRequest request;
FtpWebResponse response;
string fileName = "";
StreamReader reader;
public Form1()
{
InitializeComponent();
}
//连接按钮
private void btn_connect_Click(object sender, EventArgs e)
{
//先清空列表框
listBox1.Items.Clear();
//对FTP的操作,使用FtpWebRequest类和FtpWebResponse类
request = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.98");
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential("user", "user");
response = (FtpWebResponse)request.GetResponse();
//再使用一个StreamReader类
reader = new StreamReader(response.GetResponseStream());
string filelist = reader.ReadToEnd();
string[] list = filelist.Split(new char[]{'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
foreach(string i in list)
{
listBox1.Items.Add(i);
}
reader.Close();
response.Close();
}
//下载按钮
private void btn_down_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem == null)
{
MessageBox.Show("please chose a file!");
return;
}
fileName = (string)listBox1.SelectedItem.ToString();
string url = "D://Client//" + fileName;
if(File.Exists(url))
{
if (MessageBox.Show("The location is already exit the" + fileName + ",Click OK will be covered!", "tooltip",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
{
return;
}
}
request = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.98/" + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("user", "user");
response = (FtpWebResponse)request.GetResponse();
byte[] bt = null;
bt = DownLoadFile();
if (bt != null)
{
FileStream stream = new FileStream(url,FileMode.Create);
stream.Write(bt, 0, bt.Length);
stream.Flush();
stream.Close();
}
MessageBox.Show(fileName + "download completed!");
/*
* //非二进制传输方式,文本下载后可以打开,但视频图片使用这种方式下载都会损坏
*
request = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.98/" + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential("user", "user");
response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
reader = new StreamReader(responseStream);
StreamWriter writer = new StreamWriter(url);
writer.Write(reader.ReadToEnd());
writer.Close();
reader.Close();
MessageBox.Show(fileName + "download completed!");
*/
}
//二进制传输文件方式
public byte[] DownLoadFile()
{
try
{
Stream responseStream = response.GetResponseStream();
//将流内容写入字节数组
MemoryStream men = new MemoryStream(1024 * 500);
byte[] buffer = new byte[1024];
int bytesRead = 0;
int TotalByteRead = 0;
while (true)
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
TotalByteRead += bytesRead;
if (bytesRead == 0)
break;
men.Write(buffer, 0, bytesRead);
}
if (men.Length > 0)
{
return men.ToArray();
}
else
{
return null;
}
}
catch(Exception ep)
{
throw ep;
}
}
}
}