| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1048 人关注过本帖
标题:求c#连接ftp主机代码。谢谢
只看楼主 加入收藏
luomingchu
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2006-12-4
收藏
 问题点数:0 回复次数:1 
求c#连接ftp主机代码。谢谢
求c#连接ftp主机代码。谢谢
搜索更多相关主题的帖子: ftp 主机 代码 
2007-01-09 00:22
fququ
Rank: 1
等 级:新手上路
威 望:1
帖 子:181
专家分:0
注 册:2004-5-30
收藏
得分:0 

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;
}
}


2007-01-09 11:24
快速回复:求c#连接ftp主机代码。谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035585 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved