如下代码是关于C# FTP操作类的代码。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Utility
{
public class FtpUpDown
{

string ftpServerIP;

string ftpUserID;

string ftpPassword;

FtpWebRequest reqFTP;

{

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));

reqFTP.UseBinary = true;

reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

}

public FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword)
{
this.ftpServerIP = ftpServerIP;

this.ftpUserID = ftpUserID;

this.ftpPassword = ftpPassword;
}

{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
try
{
Connect(path);

reqFTP.Method = WRMethods;

WebResponse response = reqFTP.GetResponse();

string line = reader.ReadLine();

while (line != null)
{

result.Append(line);

result.Append("n");

line = reader.ReadLine();

}

result.Remove(result.ToString().LastIndexOf('n'), 1);

reader.Close();

response.Close();

return result.ToString().Split('n');

}

catch (Exception ex)
{
Log.WriteError("Get FileList Error:" + ex.Message);
downloadFiles = null;

return downloadFiles;
}
}

{
}
{
}

{
FileInfo fileInf = new FileInfo(filename);

reqFTP.KeepAlive = false;

reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];

int contentLen;

FileStream fs = fileInf.OpenRead();

try
{

Stream strm = reqFTP.GetRequestStream();

contentLen = fs.Read(buff, 0, buffLength);

while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);

}

strm.Close();

fs.Close();

}

catch (Exception ex)
{
Log.WriteError( "Upload Error:" + ex.Message);
}

}

{
try
{
String onlyFileName = Path.GetFileName(fileName);

string newFileName = filePath + "\" + onlyFileName;

if (File.Exists(newFileName))
{

errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName);
return false;
}
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);

FileStream outputStream = new FileStream(newFileName, FileMode.Create);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();

errorinfo = "";

return true;

}

catch (Exception ex)
{
errorinfo = string.Format("因{0},无法下载", ex.Message);

return false;

}

}

public void DeleteFileName(string fileName)
{
try
{
FileInfo fileInf = new FileInfo(fileName);

reqFTP.KeepAlive = false;

reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();

}

catch (Exception ex)
{
Log.WriteError("删除错误:" + ex.Message);
}

}

public void MakeDir(string dirName)
{
try
{

reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

response.Close();

}

catch (Exception ex)
{
Log.WriteError("创建目录错误:" + ex.Message);
}

}

public void delDir(string dirName)
{
try
{

reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

response.Close();

}

catch (Exception ex)
{
Log.WriteError("删除目录错误:" + ex.Message);
}

}

public long GetFileSize(string filename)
{

long fileSize = 0;

try
{

FileInfo fileInf = new FileInfo(filename);

reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

fileSize = response.ContentLength;

response.Close();

}

catch (Exception ex)
{
Log.WriteError("获得文件大小错误:" + ex.Message);
}

return fileSize;

}

public void Rename(string currentFilename, string newFilename)
{
try
{
FileInfo fileInf = new FileInfo(currentFilename);
reqFTP.Method = WebRequestMethods.Ftp.Rename;
reqFTP.RenameTo = newFilename;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();

}

catch (Exception ex)
{
Log.WriteError("文件改名错误:" + ex.Message);
}

}

public Stream ReadFile(string fileName)
{
try
{
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
return ftpStream;
}

catch (Exception ex)
{
Log.WriteError("读取文件错误:" + ex.Message);
return null;
}

}

public string[] GetFilesDetailList()
{

}

public string[] GetFilesDetailList(string path)
{

}

public bool fileCheckExist(string fileName)
{
bool success = false;
FtpWebResponse response = null;
StreamReader reader = null;
try
{
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
response = (FtpWebResponse)reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
if (line != null)
{
success = true;
}
}
catch (Exception)
{
success = false;
}
finally
{
if (reader != null)
{
reader.Close();
}

if (response != null)
{
response.Close();
}
}
return success;
}
}
}

C# FTP操作类的代码的更多相关文章

  1. PHP FTP操作类( 上传、拷贝、移动、删除文件/创建目录 )

    /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...

  2. C#自定义FTP访问类的代码

    如下资料是关于C#自定义FTP访问类的代码,应该对各朋友有帮助. using System; using System.Collections.Generic; using System.Text; ...

  3. php的FTP操作类

    class_ftp.php <?php /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) */ class class_ftp { public $off; // 返回操作状 ...

  4. FTP操作类

    using System; using System.Collections.Generic; using System.Net; using System.IO; namespace HGFTP { ...

  5. 很实用的FTP操作类

    using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; using ...

  6. FTP操作类的使用

    FTP(文件传输协议) FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序 ...

  7. FTP操作类(支持异步)

    public delegate void DownloadProgressChangedEventHandle(string information, long currentprogress, lo ...

  8. c#FTP操作类,包含上传,下载,删除,获取FTP文件列表文件夹等Hhelp类

    有些时间没发表文章了,之前用到过,这是我总结出来关于ftp相关操作一些方法,网上也有很多,但是没有那么全面,我的这些仅供参考和借鉴,希望能够帮助到大家,代码和相关引用我都复制粘贴出来了,希望大家喜欢 ...

  9. C# FTP操作类(获取文件和文件夹列表)

    一.如何获取某一目录下的文件和文件夹列表. 由于FtpWebRequest类只提供了WebRequestMethods.Ftp.ListDirectory方式和WebRequestMethods.Ft ...

随机推荐

  1. windows下python环境安装

    虽然是windows下安装的方式,但是linux也差不多哈: 1,首先安装python 这个是菜鸟教程的安装介绍页面,其实很清晰了:http://www.runoob.com/python3/pyth ...

  2. 大数开方 ACM-ICPC 2018 焦作赛区网络预赛 J. Participate in E-sports

    Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...

  3. python locust 性能测试:HOOKS<钩子方法>

    为locust中不同类型的事件,提供的钩子方法: from locust import TaskSet, task, events, Locust from locust.clients import ...

  4. Flask实战-留言板-使用Flask-DebugToolbar调试程序、Flask配置的两种组织形式

    使用Flask-DebugToolbar调试程序 扩展Flask-DebugToolbar提供了一系列调试功能,可以用来查看请求的SQL语句.配置选项.资源加载情况等信息.这些信息在开发时会非常有用. ...

  5. C#通过Ado.net对连接数据库并进行添加删除等常规操作的代码

    如下资料是关于C#通过Ado.net对连接数据库并进行添加删除等常规操作的内容. static string sqlcon = "server=.;database=;Integrated ...

  6. 软件综合实践Axure介绍

    首先就是下载安装Axure这款软件了,在百度上搜索“”Axure rp下载“”即可,下载完成后,打开exe安装,根据步骤一步步点击下一步即可完成安装. 运行该软件时会出现类似于填写激活码的东西,这时依 ...

  7. 关于linux系统CPU篇--->CPU使用率升高

    1.CPU使用率为单位时间内CPU使用情况的统计,以百分比的方式展示. LINUX作为一个多任务操作系统,将每个CPU的时间划分为很短的时间片,再通过调度器轮流分配给各个任务使用,因此造成多任务同时运 ...

  8. Linux的DNS配置2-主从服务器

    1.实验背景 之前写了Linux的DNS配置1-DNS入门,其中只用了一台DNS服务器,但一般在大型网络中,都要通过配置辅助DNS服务器可以提高DNS服务的可靠性,本次实验即配置DNS主从服务器 2. ...

  9. 前端框架bootstrap(响应式布局)入门

    Bootstrap,是基于HTML,CSS.javascript的前端框架 该框架已经预定义了一套CSS样式和与样式相对应的js代码(对应的样式有对应的特效.) 开发人员只需要编写HTML结构,添加b ...

  10. @Override报错的处理

    有时候我们从SVN导的项目,jre和jar包都没问题,但是就会出如下图的错误: xi 点击红叉,显示错误信息如下: 点击工具里面的window-->preferences-->java-- ...