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();
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!");
}
}
}
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();
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!");
}
}
}