c#FTP基本使用
public class FtpHelper
{
//基本设置
private static string ftppath = @"ftp://" + "192.168.1.1" + "/";
private static string username = "username";
private static string password = "password"; //获取FTP上面的文件夹和文件
public static string[] GetFolderAndFileList(string s)
{
string[] getfolderandfilelist;
FtpWebRequest request;
StringBuilder sb = new StringBuilder();
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath));
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.UseBinary = true;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
sb.Append(line);
sb.Append("\n");
Console.WriteLine(line);
line = reader.ReadLine();
}
sb.Remove(sb.ToString().LastIndexOf('\n'), );
reader.Close();
response.Close();
return sb.ToString().Split('\n');
}
catch (Exception ex)
{
Console.WriteLine("获取FTP上面的文件夹和文件:" + ex.Message);
getfolderandfilelist = null;
return getfolderandfilelist;
}
} //获取FTP上面的文件大小
public static int GetFileSize(string fileName)
{
FtpWebRequest request;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath + fileName));
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.GetFileSize;
int n = (int)request.GetResponse().ContentLength;
return n;
}
catch (Exception ex)
{
Console.WriteLine("获取FTP上面的文件大小:" + ex.Message);
return -;
}
} //FTP上传文件
public static void FileUpLoad(string filePath, string objPath = "")
{
try
{
string url = ftppath;
if (objPath != "")
url += objPath + "/";
try
{
FtpWebRequest request = null;
try
{
FileInfo fi = new FileInfo(filePath);
using (FileStream fs = fi.OpenRead())
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + fi.Name));
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
using (Stream stream = request.GetRequestStream())
{
int bufferLength = ;
byte[] buffer = new byte[bufferLength];
int i;
while ((i = fs.Read(buffer, , bufferLength)) > )
{
stream.Write(buffer, , i);
}
Console.WriteLine("FTP上传文件succesful");
}
}
}
catch (Exception ex)
{
Console.WriteLine("FTP上传文件:" + ex.Message);
}
finally
{ }
}
catch (Exception ex)
{
Console.WriteLine("FTP上传文件:" + ex.Message);
}
finally
{ }
}
catch (Exception ex)
{
Console.WriteLine("FTP上传文件:" + ex.Message);
}
} //FTP下载文件
public static void FileDownLoad(string fileName)
{
FtpWebRequest request;
try
{
string downloadPath = @"D:";
FileStream fs = new FileStream(downloadPath + "\\" + fileName, FileMode.Create);
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath + fileName));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
int bufferLength = ;
int i;
byte[] buffer = new byte[bufferLength];
i = stream.Read(buffer, , bufferLength);
while (i > )
{
fs.Write(buffer, , i);
i = stream.Read(buffer, , bufferLength);
}
stream.Close();
fs.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("FTP下载文件:" + ex.Message);
}
} //FTP删除文件
public static void FileDelete(string fileName)
{
try
{
string uri = ftppath + fileName;
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("FTP删除文件:" + ex.Message);
}
} //FTP新建目录,上一级须先存在
public static void MakeDir(string dirName)
{
try
{
string uri = ftppath + dirName;
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("FTP新建目录:" + ex.Message);
}
} //FTP删除目录,上一级须先存在
public static void DelDir(string dirName)
{
try
{
string uri = ftppath + dirName;
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("FTP删除目录:" + ex.Message);
}
}
}
c#FTP基本使用的更多相关文章
- 8.仿阿里云虚拟云服务器的FTP(包括FTP文件夹大小限制)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 原文:http://dnt.dkill.net/Ar ...
- Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...
- 阿里云学生优惠Windows Server 2012 R2安装IIS,ftp等组件,绑定服务器域名,域名解析到服务器,域名备案,以及安装期间错误的解决方案
前言: 这几天终于还是按耐不住买了一个月阿里云的学生优惠.只要是学生,在学信网上注册过,并且支付宝实名认证,就可以用9块9的价格买阿里云的云服务ECS.确实是相当的优惠. 我买的是Windows S ...
- win7下利用ftp实现华为路由器的上传和下载
win7下利用ftp实现华为路由器的上传和下载 1. Win7下ftp的安装和配置 (1)开始->控制面板->程序->程序和功能->打开或关闭Windows功能 (2)在Wi ...
- Java实现FTP文件与文件夹的上传和下载
Java实现FTP文件与文件夹的上传和下载 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制 ...
- centos下开启ftp服务
如果要ftp访问linux需要安装ftp服务,vsftpd是Linux下比较好的的FTP服务器. 一.检查安装vsftp //检查是否安装vsftpd rpm -qa | grep vsftpd // ...
- 解决开启服务器防火墙导致ftp不能连接的问题
在防火墙设置的"高级"选项卡中的"网络连接设置"--"本地连接"--"设置"中添加了"FTP服务器" ...
- centos6.5 nginx-1.8.0和ftp搭建图片服务器
一.Nginx的安装步骤 1.Nginx安装环境: gcc: 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c+ ...
- Jenkins配置MSBuild实现自动部署(MSBuild+SVN/Subversion+FTP+BAT)
所要用到的主要插件: [MSBuild Plugin] 具体操作: 1.配置MSBuild的版本 [系统管理]->[Global Tool Configuration]->[MSBuild ...
- [CentOs7]搭建ftp服务器(2)——添加用户
摘要 上篇文章完成了ftp服务器的安装与匿名访问的内容,当然出于安全的考虑是不允许匿名访问服务器的,所以就有了本篇的内容 ,为ftp服务器添加用户,用改用户进行访问. vsftpd添加用户 FTP用户 ...
随机推荐
- 【WPF学习】第三十六章 样式基础
前面三章介绍了WPF资源系统,使用资源可在一个地方定义对象而在整个标记中重用他们.尽管可使用资源存储各种对象,但使用资源最常见的原因之一是通过他们的保存样式. 样式是可应用于元素的属性值集合.WPF样 ...
- 实验15: STP
实验12: STP Ø 实验目的通过本实验,读者可以掌握如下技能:(1) 理解STP 的工作原理(2) 掌握STP 树的控制(3) 利用PVST 进行负载平衡 Ø 实验拓扑 SW1 和S ...
- 源码级别gdb远程调试(实现OS简单内核)
最近在学着编写一个操作系统的简单内核,需要debug工具,我们这里使用gdb来进行调试,由于虚拟机运行和本机是两个部分,所以使用 gdb 的远程调试技术,这里对 gdb 常见调试以及远程调试方式做一个 ...
- 批处理版MPlayer播放器(甲兵时代原创批处理)(下)
注意,由于空间不支持显示退格键,需要自己手动补上,方法如上图: 接上篇: 批处理版音视频播放器上(甲兵时代原创批处理) :Bc cls COLOR 2F echo. call :colour &quo ...
- Codeforces_492_E
http://codeforces.com/problemset/problem/492/E 题目规定了gcd=1,可以在纸上模拟一下,发现每一个起点,都会经历过n个点,n个点都是不同行不同列.可以把 ...
- HYSBZ_1588_set
http://www.lydsy.com/JudgeOnline/problem.php?id=1588 直接set做就好了,注意set的迭代器只能--之类的操作. OJ崩了,也不知道对错. #inc ...
- 《剑指Offer》第二章(一)题3-8
为春招实习做准备,记录一下<剑指Offer>里面的面试题 第二章 面试题3:数组之中的重复数字. 这个题吧,虽然不难,但是不知道为什么就是看了很久,可能很久没有做算法题了.最后面一句话说的 ...
- How to check sqlsever table data type identity status ?
Unlike in Oracle, sqlserver has an special data type in order by make identity growth. But what abou ...
- Spring IOC容器源码分析
注:本文转自https://javadoop.com/post/spring-ioc Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容 ...
- Python原来这么好学-1.1节: 在windows中安装Python
这是一本教同学们彻底学通Python的高质量学习教程,认真地学习每一章节的内容,每天只需学好一节,帮助你成为一名卓越的Python程序员: 本教程面向的是零编程基础的同学,非科班人士,以及有一定编程水 ...