public partial class FtpClientForm : Form
{
private string serverDirectory;
public FtpClientForm()
{
InitializeComponent();
}
private void FillDirectoryList(Stream stream)
{
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();
string[] files = content.Split('\n');
listBox1.DataSource = files;
reader.Close();
}
//打开目录并填充列表框
private void OnOpen(object sender, EventArgs e)
{
Cursor currentCursor = this.Cursor;
FtpWebResponse response = null;
Stream stream = null;
try
{
this.Cursor = Cursors.WaitCursor;
//创建ftpwebrequest对象
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(this.txtServer.Text.Trim());
request.Credentials = new NetworkCredential(txtUserName.Text, txtPwd.Text);
request.Method = WebRequestMethods.Ftp.ListDirectory;
//发送请求到服务器
response = (FtpWebResponse)request.GetResponse();
//读取返回信息,并填充列表框
stream = response.GetResponseStream();
FillDirectoryList(stream);
serverDirectory = null;
btnOpenDirectory.Enabled = false;
btnGetFile.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ftp错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (response != null)
response.Close();
if (stream != null)
stream.Close();
this.Cursor = currentCursor;
}
}
//打开目录
private void OnOpenDirectory(object sender, EventArgs e)
{
FtpWebResponse response = null;
Stream stream = null;
try
{
string subDirectory = listBox1.SelectedValue.ToString().Trim();
serverDirectory += "/" + subDirectory;
Uri uri = new Uri(new Uri(txtServer.Text), serverDirectory);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(txtUserName.Text, txtPwd.Text);
request.Method = WebRequestMethods.Ftp.ListDirectory;
response = (FtpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
FillDirectoryList(stream);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ftp错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (response != null)
response.Close();
if (stream != null)
stream.Close();
}
}
//下载文件
private void OnDownloadFile(object sender, EventArgs e)
{
FtpWebResponse response = null;
Stream inStream = null;
Stream outStream = null;
try
{
Uri baseUri = new Uri(txtServer.Text);
string filename=listBox1.SelectedValue.ToString().Trim();
string fullFilename=this.txtServer.Text +"//"+filename;
Uri uri=new Uri(baseUri,fullFilename);
FtpWebRequest request=(FtpWebRequest)WebRequest.Create(uri);
request.Credentials=new NetworkCredential(txtUserName.Text,txtPwd.Text);
request.Method=WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = cbxBinary.Checked;
response = (FtpWebResponse)request.GetResponse();
inStream = response.GetResponseStream();
saveFileDialog1.FileName = filename;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
outStream = File.OpenWrite(saveFileDialog1.FileName);
byte[] buffer = new byte[4096];
int size = 0;
while((size=inStream.Read(buffer,0,4096))>0)
{
outStream.Write(buffer,0,size);
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "ftp错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (outStream != null)
outStream.Close();
if (response != null)
response.Close();
if (inStream != null)
inStream.Close();
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
btnGetFile.Enabled = true;
btnOpenDirectory.Enabled = true;
}
}