|
|
@@ -1,4 +1,5 @@
|
|
|
using System;
|
|
|
+using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Net;
|
|
|
using System.Text;
|
|
|
@@ -17,24 +18,61 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// Ftp服务器ip
|
|
|
/// </summary>
|
|
|
- public static string FtpServer { get; set; }
|
|
|
+ private string FtpServer { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// Ftp 指定用户名
|
|
|
/// </summary>
|
|
|
- public static string Username { get; set; }
|
|
|
+ private string Username { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// Ftp 指定用户密码
|
|
|
/// </summary>
|
|
|
- public static string Password { get; set; }
|
|
|
+ private string Password { get; set; }
|
|
|
+
|
|
|
+ #endregion
|
|
|
|
|
|
/// <summary>
|
|
|
- /// ftp地址
|
|
|
+ /// 获取一个匿名登录的ftp客户端
|
|
|
/// </summary>
|
|
|
- public static string FtpUri = "ftp://" + FtpServer + "/";
|
|
|
+ /// <param name="serverIp">服务器IP地址</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static FtpClient GetAnonymousClient(string serverIp)
|
|
|
+ {
|
|
|
+ if (!serverIp.MatchInetAddress())
|
|
|
+ {
|
|
|
+ throw new ArgumentException("IP地址格式不正确");
|
|
|
+ }
|
|
|
|
|
|
- #endregion
|
|
|
+ FtpClient ftpClient = new FtpClient
|
|
|
+ {
|
|
|
+ FtpServer = serverIp
|
|
|
+ };
|
|
|
+ return ftpClient;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取一个匿名登录的ftp客户端
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="serverIp">服务器ip</param>
|
|
|
+ /// <param name="username">用户名</param>
|
|
|
+ /// <param name="password">密码</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static FtpClient GetClient(string serverIp, string username, string password)
|
|
|
+ {
|
|
|
+ if (!serverIp.MatchInetAddress())
|
|
|
+ {
|
|
|
+ throw new ArgumentException("IP地址格式不正确");
|
|
|
+ }
|
|
|
+
|
|
|
+ FtpClient ftpClient = new FtpClient
|
|
|
+ {
|
|
|
+ FtpServer = serverIp,
|
|
|
+ Username = username,
|
|
|
+ Password = password
|
|
|
+ };
|
|
|
+ return ftpClient;
|
|
|
+ }
|
|
|
|
|
|
#region 从FTP服务器下载文件,指定本地路径和本地文件名
|
|
|
|
|
|
@@ -45,7 +83,7 @@ namespace Masuit.Tools.Net
|
|
|
/// <param name="localFileName">保存本地的文件名(包含路径)</param>
|
|
|
/// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
|
|
|
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
|
|
|
- public static void Download(string remoteFileName, string localFileName, bool ifCredential = false, Action<int, int> updateProgress = null)
|
|
|
+ public void Download(string remoteFileName, string localFileName, bool ifCredential = false, Action<int, int> updateProgress = null)
|
|
|
{
|
|
|
using (FileStream outputStream = new FileStream(localFileName, FileMode.Create))
|
|
|
{
|
|
|
@@ -108,7 +146,7 @@ namespace Masuit.Tools.Net
|
|
|
/// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
|
|
|
/// <param name="size">已下载文件流大小</param>
|
|
|
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
|
|
|
- public static void BrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress = null)
|
|
|
+ public void BrokenDownload(string remoteFileName, string localFileName, bool ifCredential, long size, Action<int, int> updateProgress = null)
|
|
|
{
|
|
|
using (FileStream outputStream = new FileStream(localFileName, FileMode.Append))
|
|
|
{
|
|
|
@@ -170,7 +208,7 @@ namespace Masuit.Tools.Net
|
|
|
/// <param name="ifCredential">是否启用身份验证(false:表示允许用户匿名下载)</param>
|
|
|
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
|
|
|
/// <param name="brokenOpen">是否断点下载:true 会在localFileName 找是否存在已经下载的文件,并计算文件流大小</param>
|
|
|
- public static void Download(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress = null)
|
|
|
+ public void Download(string remoteFileName, string localFileName, bool ifCredential, bool brokenOpen, Action<int, int> updateProgress = null)
|
|
|
{
|
|
|
if (brokenOpen)
|
|
|
{
|
|
|
@@ -196,9 +234,10 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// 上传文件到FTP服务器
|
|
|
/// </summary>
|
|
|
+ /// <param name="relativePath">相对目录</param>
|
|
|
/// <param name="localFullPathName">本地带有完整路径的文件名</param>
|
|
|
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
|
|
|
- public static void UploadFile(string localFullPathName, Action<int, int> updateProgress = null)
|
|
|
+ public void UploadFile(string relativePath, string localFullPathName, Action<int, int> updateProgress = null)
|
|
|
{
|
|
|
FileInfo finfo = new FileInfo(localFullPathName);
|
|
|
if (FtpServer == null || FtpServer.Trim().Length == 0)
|
|
|
@@ -206,7 +245,7 @@ namespace Masuit.Tools.Net
|
|
|
throw new Exception("ftp上传目标服务器地址未设置!");
|
|
|
}
|
|
|
|
|
|
- Uri uri = new Uri("ftp://" + FtpServer + "/" + finfo.Name);
|
|
|
+ Uri uri = new Uri("ftp://" + FtpServer + "/" + relativePath + "/" + finfo.Name);
|
|
|
var reqFtp = (FtpWebRequest)WebRequest.Create(uri);
|
|
|
reqFtp.KeepAlive = false;
|
|
|
reqFtp.UseBinary = true;
|
|
|
@@ -243,7 +282,7 @@ namespace Masuit.Tools.Net
|
|
|
/// <param name="remoteFilepath">远程文件所在文件夹路径</param>
|
|
|
/// <param name="updateProgress">报告进度的处理(第一个参数:总大小,第二个参数:当前进度)</param>
|
|
|
/// <returns></returns>
|
|
|
- public static bool UploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress = null)
|
|
|
+ public bool UploadBroken(string localFullPath, string remoteFilepath, Action<int, int> updateProgress = null)
|
|
|
{
|
|
|
if (remoteFilepath == null)
|
|
|
{
|
|
|
@@ -327,7 +366,7 @@ namespace Masuit.Tools.Net
|
|
|
/// </summary>
|
|
|
/// <param name="str"></param>
|
|
|
/// <returns></returns>
|
|
|
- private static string RemoveSpaces(string str)
|
|
|
+ private string RemoveSpaces(string str)
|
|
|
{
|
|
|
string a = "";
|
|
|
foreach (char c in str)
|
|
|
@@ -346,14 +385,14 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// 获取已上传文件大小
|
|
|
/// </summary>
|
|
|
- /// <param name="filename">文件名称</param>
|
|
|
+ /// <param name="filePath">文件名称</param>
|
|
|
/// <param name="remoteFilepath">服务器文件路径</param>
|
|
|
/// <returns></returns>
|
|
|
- public static long GetFileSize(string filename, string remoteFilepath)
|
|
|
+ public long GetFileSize(string filePath, string remoteFilepath)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- FileInfo fi = new FileInfo(filename);
|
|
|
+ FileInfo fi = new FileInfo(filePath);
|
|
|
string uri;
|
|
|
if (remoteFilepath.Length == 0)
|
|
|
{
|
|
|
@@ -387,15 +426,15 @@ namespace Masuit.Tools.Net
|
|
|
/// 获取当前目录下明细(包含文件和文件夹)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
- public static string[] GetFilesDetails()
|
|
|
+ public string[] GetFilesDetails(string relativePath = "")
|
|
|
{
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
- var ftp = (FtpWebRequest)WebRequest.Create(new Uri(FtpUri));
|
|
|
+ var ftp = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine("ftp://" + FtpServer, relativePath).Replace("\\", "/")));
|
|
|
ftp.Credentials = new NetworkCredential(Username, Password);
|
|
|
ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
|
|
|
using (WebResponse response = ftp.GetResponse())
|
|
|
{
|
|
|
- using (StreamReader reader = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(), Encoding.Default))
|
|
|
+ using (StreamReader reader = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(), Encoding.UTF8))
|
|
|
{
|
|
|
string line = reader.ReadLine();
|
|
|
while (line != null)
|
|
|
@@ -416,16 +455,16 @@ namespace Masuit.Tools.Net
|
|
|
/// 获取当前目录下文件列表(仅文件)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
- public static string[] GetFiles(string mask)
|
|
|
+ public List<string> GetFiles(string relativePath = "", string mask = "*.*")
|
|
|
{
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
- var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(FtpUri));
|
|
|
+ var result = new List<string>();
|
|
|
+ var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine("ftp://" + FtpServer, relativePath).Replace("\\", "/")));
|
|
|
reqFtp.UseBinary = true;
|
|
|
reqFtp.Credentials = new NetworkCredential(Username, Password);
|
|
|
reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
|
|
|
using (WebResponse response = reqFtp.GetResponse())
|
|
|
{
|
|
|
- using (StreamReader reader = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(), Encoding.Default))
|
|
|
+ using (StreamReader reader = new StreamReader(response.GetResponseStream() ?? throw new InvalidOperationException(), Encoding.UTF8))
|
|
|
{
|
|
|
string line = reader.ReadLine();
|
|
|
while (line != null)
|
|
|
@@ -435,33 +474,29 @@ namespace Masuit.Tools.Net
|
|
|
string temp = mask.Substring(0, mask.IndexOf("*", StringComparison.Ordinal));
|
|
|
if (line.Substring(0, temp.Length) == temp)
|
|
|
{
|
|
|
- result.Append(line);
|
|
|
- result.Append("\n");
|
|
|
+ result.Add(line);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- result.Append(line);
|
|
|
- result.Append("\n");
|
|
|
+ result.Add(line);
|
|
|
}
|
|
|
|
|
|
line = reader.ReadLine();
|
|
|
}
|
|
|
-
|
|
|
- result.Remove(result.ToString().LastIndexOf('\n'), 1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return result.ToString().Split('\n');
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取当前目录下所有的文件夹列表(仅文件夹)
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
- public static string[] GetDirectories()
|
|
|
+ public string[] GetDirectories(string relativePath)
|
|
|
{
|
|
|
- string[] drectory = GetFilesDetails();
|
|
|
+ string[] drectory = GetFilesDetails(relativePath);
|
|
|
string m = string.Empty;
|
|
|
foreach (string str in drectory)
|
|
|
{
|
|
|
@@ -471,12 +506,13 @@ namespace Masuit.Tools.Net
|
|
|
/*判断 Windows 风格*/
|
|
|
m += str.Substring(dirPos + 5).Trim() + "\n";
|
|
|
}
|
|
|
- else if (str.Trim().Substring(0, 1).ToUpper() == "D")
|
|
|
+ else if (str.Trim().StartsWith("d"))
|
|
|
{
|
|
|
/*判断 Unix 风格*/
|
|
|
- string dir = str.Substring(54).Trim();
|
|
|
+ string dir = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)[8];
|
|
|
if (dir != "." && dir != "..")
|
|
|
{
|
|
|
+ dir = str.Substring(str.IndexOf(dir, StringComparison.Ordinal));
|
|
|
m += dir + "\n";
|
|
|
}
|
|
|
}
|
|
|
@@ -488,7 +524,6 @@ namespace Masuit.Tools.Net
|
|
|
};
|
|
|
return m.Split(n);
|
|
|
}
|
|
|
-
|
|
|
#endregion
|
|
|
|
|
|
#region 删除文件及文件夹
|
|
|
@@ -496,10 +531,10 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// 删除文件
|
|
|
/// </summary>
|
|
|
- /// <param name="fileName"></param>
|
|
|
- public static void Delete(string fileName)
|
|
|
+ /// <param name="filePath"></param>
|
|
|
+ public void Delete(string filePath)
|
|
|
{
|
|
|
- string uri = FtpUri + fileName;
|
|
|
+ string uri = Path.Combine("ftp://" + FtpServer, filePath).Replace("\\", "/");
|
|
|
var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(uri));
|
|
|
|
|
|
reqFtp.Credentials = new NetworkCredential(Username, Password);
|
|
|
@@ -521,10 +556,10 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// 删除文件夹
|
|
|
/// </summary>
|
|
|
- /// <param name="folderName"></param>
|
|
|
- public static void RemoveDirectory(string folderName)
|
|
|
+ /// <param name="dirPath"></param>
|
|
|
+ public void RemoveDirectory(string dirPath)
|
|
|
{
|
|
|
- string uri = FtpUri + folderName;
|
|
|
+ string uri = Path.Combine("ftp://" + FtpServer, dirPath).Replace("\\", "/");
|
|
|
var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(uri));
|
|
|
reqFtp.Credentials = new NetworkCredential(Username, Password);
|
|
|
reqFtp.KeepAlive = false;
|
|
|
@@ -548,11 +583,11 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// 获取指定文件大小
|
|
|
/// </summary>
|
|
|
- /// <param name="filename"></param>
|
|
|
+ /// <param name="filePath"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static long GetFileSize(string filename)
|
|
|
+ public long GetFileSize(string filePath)
|
|
|
{
|
|
|
- var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(FtpUri + filename));
|
|
|
+ var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine("ftp://" + FtpServer, filePath).Replace("\\", "/")));
|
|
|
reqFtp.Method = WebRequestMethods.Ftp.GetFileSize;
|
|
|
reqFtp.UseBinary = true;
|
|
|
reqFtp.Credentials = new NetworkCredential(Username, Password);
|
|
|
@@ -566,15 +601,15 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// 判断当前目录下指定的子目录是否存在
|
|
|
/// </summary>
|
|
|
- /// <param name="remoteDirectoryName">指定的目录名</param>
|
|
|
- public bool DirectoryExist(string remoteDirectoryName)
|
|
|
+ /// <param name="remoteDirPath">指定的目录名</param>
|
|
|
+ public bool DirectoryExist(string remoteDirPath)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- string[] dirList = GetDirectories();
|
|
|
+ string[] dirList = GetDirectories(remoteDirPath);
|
|
|
foreach (string str in dirList)
|
|
|
{
|
|
|
- if (str.Trim() == remoteDirectoryName.Trim())
|
|
|
+ if (str.Trim() == remoteDirPath.Trim())
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
@@ -594,7 +629,7 @@ namespace Masuit.Tools.Net
|
|
|
/// <param name="remoteFileName">远程文件名</param>
|
|
|
public bool FileExist(string remoteFileName)
|
|
|
{
|
|
|
- string[] fileList = GetFiles("*.*");
|
|
|
+ var fileList = GetFiles("*.*");
|
|
|
foreach (string str in fileList)
|
|
|
{
|
|
|
if (str.Trim() == remoteFileName.Trim())
|
|
|
@@ -609,11 +644,12 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// 创建文件夹
|
|
|
/// </summary>
|
|
|
- /// <param name="dirName"></param>
|
|
|
- public void MakeDir(string dirName)
|
|
|
+ /// <param name="relativePath">路径</param>
|
|
|
+ /// <param name="newDir">新建文件夹</param>
|
|
|
+ public void MakeDir(string relativePath, string newDir)
|
|
|
{
|
|
|
// dirName = name of the directory to create.
|
|
|
- var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(FtpUri + dirName));
|
|
|
+ var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine("ftp://" + FtpServer, relativePath, newDir).Replace("\\", "/")));
|
|
|
reqFtp.Method = WebRequestMethods.Ftp.MakeDirectory;
|
|
|
reqFtp.UseBinary = true;
|
|
|
reqFtp.Credentials = new NetworkCredential(Username, Password);
|
|
|
@@ -628,11 +664,12 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// 改名
|
|
|
/// </summary>
|
|
|
+ /// <param name="relativePath">相对路径</param>
|
|
|
/// <param name="currentFilename"></param>
|
|
|
/// <param name="newFilename"></param>
|
|
|
- public void Rename(string currentFilename, string newFilename)
|
|
|
+ public void Rename(string relativePath, string currentFilename, string newFilename)
|
|
|
{
|
|
|
- var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(FtpUri + currentFilename));
|
|
|
+ var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine("ftp://" + FtpServer, relativePath, currentFilename).Replace("\\", "/")));
|
|
|
reqFtp.Method = WebRequestMethods.Ftp.Rename;
|
|
|
reqFtp.RenameTo = newFilename;
|
|
|
reqFtp.UseBinary = true;
|
|
|
@@ -648,32 +685,13 @@ namespace Masuit.Tools.Net
|
|
|
/// <summary>
|
|
|
/// 移动文件
|
|
|
/// </summary>
|
|
|
+ /// <param name="relativePath">相对路径</param>
|
|
|
/// <param name="currentFilename"></param>
|
|
|
/// <param name="newDirectory"></param>
|
|
|
- public void MoveFile(string currentFilename, string newDirectory)
|
|
|
- {
|
|
|
- Rename(currentFilename, newDirectory);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 切换当前目录
|
|
|
- /// </summary>
|
|
|
- /// <param name="directoryName"></param>
|
|
|
- /// <param name="isRoot">true 绝对路径 false 相对路径</param>
|
|
|
- public void GotoDirectory(string directoryName, bool isRoot)
|
|
|
+ public void MoveFile(string relativePath, string currentFilename, string newDirectory)
|
|
|
{
|
|
|
- if (isRoot)
|
|
|
- {
|
|
|
- _ftpRemotePath = directoryName;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- _ftpRemotePath += directoryName + "/";
|
|
|
- }
|
|
|
-
|
|
|
- FtpUri = "ftp://" + FtpServer + "/" + _ftpRemotePath + "/";
|
|
|
+ Rename(relativePath, currentFilename, newDirectory);
|
|
|
}
|
|
|
-
|
|
|
#endregion
|
|
|
}
|
|
|
}
|