video/FtpHelper.cs
2024-12-11 19:48:43 +08:00

268 lines
9.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace videoGather
{
public class FtpHelper
{
//基本设置
//private static string ftppath = @"ftp://" + "10.13.1.36" + "/";
private static string ftppath = ConfigurationManager.AppSettings["ftppath"];
private static string username = ConfigurationManager.AppSettings["username"];
private static string password = ConfigurationManager.AppSettings["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'), 1);
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 -1;
}
}
//FTP上传文件
public static string 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 = 5120;
byte[] buffer = new byte[bufferLength];
int i;
while ((i = fs.Read(buffer, 0, bufferLength)) > 0)
{
stream.Write(buffer, 0, i);
}
return "上传成功";
}
}
}
catch (Exception ex)
{
return ex.ToString();
}
finally
{
}
}
catch (Exception ex)
{
return ex.ToString();
}
finally
{
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
//FTP上传文件
public static string FileUpLoad(string filePath, string objPath ,string ftpFileName)
{
try
{
string url = ftppath;
if (objPath != "")
url += objPath + "/";
FtpWebRequest request = null;
FileInfo fi = new FileInfo(filePath);
using (FileStream fs = fi.OpenRead())
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + ftpFileName));
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.UsePassive = true;//被动传输模式 设置
using (Stream stream = request.GetRequestStream())
{
int bufferLength = 5120;
byte[] buffer = new byte[bufferLength];
int i;
while ((i = fs.Read(buffer, 0, bufferLength)) > 0)
{
stream.Write(buffer, 0, i);
}
return "上传成功";
}
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
//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 = 5120;
int i;
byte[] buffer = new byte[bufferLength];
i = stream.Read(buffer, 0, bufferLength);
while (i > 0)
{
fs.Write(buffer, 0, i);
i = stream.Read(buffer, 0, 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);
}
}
}
}