📄 clsftp.cs
字号:
return true;
}
}
return false;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 删除文件#region 删除文件
/// <summary>
/// 从FTP服务器上面删除一个文件
/// </summary>
/// <param name="RemoteFileName">远程文件名</param>
public void DeleteFile(string RemoteFileName)
{
try
{
if (!IsValidFileChars(RemoteFileName))
{
throw new Exception("文件名非法!");
}
Response = Open(new Uri(this.Uri.ToString() + RemoteFileName), WebRequestMethods.Ftp.DeleteFile);
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 重命名文件#region 重命名文件
/// <summary>
/// 更改一个文件的名称或一个目录的名称
/// </summary>
/// <param name="RemoteFileName">原始文件或目录名称</param>
/// <param name="NewFileName">新的文件或目录的名称</param>
public bool ReName(string RemoteFileName, string NewFileName)
{
try
{
if (!IsValidFileChars(RemoteFileName) || !IsValidFileChars(NewFileName))
{
throw new Exception("文件名非法!");
}
if (RemoteFileName == NewFileName)
{
return true;
}
if (FileExist(RemoteFileName))
{
Request = OpenRequest(new Uri(this.Uri.ToString() + RemoteFileName), WebRequestMethods.Ftp.Rename);
Request.RenameTo = NewFileName;
Response = (FtpWebResponse)Request.GetResponse();
}
else
{
throw new Exception("文件在服务器上不存在!");
}
return true;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 拷贝、移动文件#region 拷贝、移动文件
/// <summary>
/// 把当前目录下面的一个文件拷贝到服务器上面另外的目录中,注意,拷贝文件之后,当前工作目录还是文件原来所在的目录
/// </summary>
/// <param name="RemoteFile">当前目录下的文件名</param>
/// <param name="DirectoryName">新目录名称。
/// 说明:如果新目录是当前目录的子目录,则直接指定子目录。如: SubDirectory1/SubDirectory2 ;
/// 如果新目录不是当前目录的子目录,则必须从根目录一级一级的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2
/// </param>
/// <returns></returns>
public bool CopyFileToAnotherDirectory(string RemoteFile, string DirectoryName)
{
string CurrentWorkDir = this.DirectoryPath;
try
{
byte[] bt = DownloadFile(RemoteFile);
GotoDirectory(DirectoryName);
bool Success = UploadFile(bt, RemoteFile, false);
this.DirectoryPath = CurrentWorkDir;
return Success;
}
catch (Exception ep)
{
this.DirectoryPath = CurrentWorkDir;
ErrorMsg = ep.ToString();
throw ep;
}
}
/// <summary>
/// 把当前目录下面的一个文件移动到服务器上面另外的目录中,注意,移动文件之后,当前工作目录还是文件原来所在的目录
/// </summary>
/// <param name="RemoteFile">当前目录下的文件名</param>
/// <param name="DirectoryName">新目录名称。
/// 说明:如果新目录是当前目录的子目录,则直接指定子目录。如: SubDirectory1/SubDirectory2 ;
/// 如果新目录不是当前目录的子目录,则必须从根目录一级一级的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2
/// </param>
/// <returns></returns>
public bool MoveFileToAnotherDirectory(string RemoteFile, string DirectoryName)
{
string CurrentWorkDir = this.DirectoryPath;
try
{
if (DirectoryName == "")
return false;
if (!DirectoryName.StartsWith("/"))
DirectoryName = "/" + DirectoryName;
if (!DirectoryName.EndsWith("/"))
DirectoryName += "/";
bool Success = ReName(RemoteFile, DirectoryName + RemoteFile);
this.DirectoryPath = CurrentWorkDir;
return Success;
}
catch (Exception ep)
{
this.DirectoryPath = CurrentWorkDir;
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 建立、删除子目录#region 建立、删除子目录
/// <summary>
/// 在FTP服务器上当前工作目录建立一个子目录
/// </summary>
/// <param name="DirectoryName">子目录名称</param>
public bool MakeDirectory(string DirectoryName)
{
try
{
if (!IsValidPathChars(DirectoryName))
{
throw new Exception("目录名非法!");
}
if (DirectoryExist(DirectoryName))
{
throw new Exception("服务器上面已经存在同名的文件名或目录名!");
}
Response = Open(new Uri(this.Uri.ToString() + DirectoryName), WebRequestMethods.Ftp.MakeDirectory);
return true;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
/// <summary>
/// 从当前工作目录中删除一个子目录
/// </summary>
/// <param name="DirectoryName">子目录名称</param>
public bool RemoveDirectory(string DirectoryName)
{
try
{
if (!IsValidPathChars(DirectoryName))
{
throw new Exception("目录名非法!");
}
if (!DirectoryExist(DirectoryName))
{
throw new Exception("服务器上面不存在指定的文件名或目录名!");
}
Response = Open(new Uri(this.Uri.ToString() + DirectoryName), WebRequestMethods.Ftp.RemoveDirectory);
return true;
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
#endregion
#region 文件、目录名称有效性判断#region 文件、目录名称有效性判断
/// <summary>
/// 判断目录名中字符是否合法
/// </summary>
/// <param name="DirectoryName">目录名称</param>
public bool IsValidPathChars(string DirectoryName)
{
char[] invalidPathChars = Path.GetInvalidPathChars();
char[] DirChar = DirectoryName.ToCharArray();
foreach (char C in DirChar)
{
if (Array.BinarySearch(invalidPathChars, C) >= 0)
{
return false;
}
}
return true;
}
/// <summary>
/// 判断文件名中字符是否合法
/// </summary>
/// <param name="FileName">文件名称</param>
public bool IsValidFileChars(string FileName)
{
char[] invalidFileChars = Path.GetInvalidFileNameChars();
char[] NameChar = FileName.ToCharArray();
foreach (char C in NameChar)
{
if (Array.BinarySearch(invalidFileChars, C) >= 0)
{
return false;
}
}
return true;
}
#endregion
#region 目录切换操作#region 目录切换操作
/// <summary>
/// 进入一个目录
/// </summary>
/// <param name="DirectoryName">
/// 新目录的名字。
/// 说明:如果新目录是当前目录的子目录,则直接指定子目录。如: SubDirectory1/SubDirectory2 ;
/// 如果新目录不是当前目录的子目录,则必须从根目录一级一级的指定。如: ./NewDirectory/SubDirectory1/SubDirectory2
/// </param>
public bool GotoDirectory(string DirectoryName)
{
string CurrentWorkPath = this.DirectoryPath;
try
{
DirectoryName = DirectoryName.Replace("\\", "/");
string[] DirectoryNames = DirectoryName.Split(new char[] { '/' });
if (DirectoryNames[0] == ".")
{
this.DirectoryPath = "/";
if (DirectoryNames.Length == 1)
{
return true;
}
Array.Clear(DirectoryNames, 0, 1);
}
bool Success = false;
foreach (string dir in DirectoryNames)
{
if (dir != null)
{
Success = EnterOneSubDirectory(dir);
if (!Success)
{
this.DirectoryPath = CurrentWorkPath;
return false;
}
}
}
return Success;
}
catch (Exception ep)
{
this.DirectoryPath = CurrentWorkPath;
ErrorMsg = ep.ToString();
throw ep;
}
}
/// <summary>
/// 从当前工作目录进入一个子目录
/// </summary>
/// <param name="DirectoryName">子目录名称</param>
private bool EnterOneSubDirectory(string DirectoryName)
{
try
{
if (DirectoryName.IndexOf("/") >= 0 || !IsValidPathChars(DirectoryName))
{
throw new Exception("目录名非法!");
}
if (DirectoryName.Length > 0 && DirectoryExist(DirectoryName))
{
if (!DirectoryName.EndsWith("/"))
{
DirectoryName += "/";
}
_DirectoryPath += DirectoryName;
return true;
}
else
{
return false;
}
}
catch (Exception ep)
{
ErrorMsg = ep.ToString();
throw ep;
}
}
/// <summary>
/// 从当前工作目录往上一级目录
/// </summary>
public bool ComeoutDirectory()
{
if (_DirectoryPath == "/")
{
ErrorMsg = "当前目录已经是根目录!";
throw new Exception("当前目录已经是根目录!");
}
char[] sp = new char[1] { '/' };
string[] strDir = _DirectoryPath.Split(sp, StringSplitOptions.RemoveEmptyEntries);
if (strDir.Length == 1)
{
_DirectoryPath = "/";
}
else
{
_DirectoryPath = String.Join("/", strDir, 0, strDir.Length - 1);
}
return true;
}
#endregion
#region 重载WebClient,支持FTP进度#region 重载WebClient,支持FTP进度
internal class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
FtpWebRequest req = (FtpWebRequest)base.GetWebRequest(address);
req.UsePassive = false;
return req;
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -