没有合适的资源?快使用搜索试试~ 我知道了~
首页C#操作ftp类完整实例
资源详情
资源评论
资源推荐

C#操作操作ftp类完整实例类完整实例
主要介绍了C#操作ftp类,以一个完整实例形式详细分析了C#操作FTP文件传输所涉及的FTP连接、文件传输、参
数设置、文件删除等技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了C#操作ftp类。分享给大家供大家参考。具体如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Globalization;
namespace FtpTest1
{
public class FtpWeb
{
string ftpServerIP;
string ftpRemotePath;
string ftpUserID;
string ftpPassword;
string ftpURI;
/// <summary>
/// 连接FTP
/// </summary>
/// <param name="FtpServerIP">FTP连接地址</param>
/// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>
/// <param name="FtpUserID">用户名</param>
/// <param name="FtpPassword">密码</param>
public FtpWeb(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
{
ftpServerIP = FtpServerIP;
ftpRemotePath = FtpRemotePath;
ftpUserID = FtpUserID;
ftpPassword = FtpPassword;
ftpURI = "ftp://" + ftpServerIP + "/" ;
}
static void Main() {
//string file = "c:\aq3.gifa";
//FileInfo fileInf = new FileInfo(file);
//if (!fileInf.Exists)
//{
// Console.WriteLine(file + " no exists");
//}
//else {
// Console.WriteLine("yes");
//}
//Console.ReadLine();
FtpWeb fw = new FtpWeb("121.11.65.10", "", "aa1", "aa");
string[] filePaths = { "c:\aq3.gif1", "c:\aq2.gif1", "c:\bsmain_runtime.log" };
Console.WriteLine(fw.UploadFile(filePaths));
Console.ReadLine();
}
//上传文件
public string UploadFile( string[] filePaths ) {
StringBuilder sb = new StringBuilder();
if ( filePaths != null && filePaths.Length > 0 ){
foreach( var file in filePaths ){
sb.Append(Upload( file ));
}
}
return sb.ToString();
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="filename"></param>
private string Upload(string filename)
{
FileInfo fileInf = new FileInfo(filename);
if ( !fileInf.Exists ){
return filename + " 不存在!";
}
string uri = ftpURI + fileInf.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;











安全验证
文档复制为VIP权益,开通VIP直接复制

评论1