{
string[] downloadFiles;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
ftp.UseBinary = true;
ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);//如果是匿名登录,,则注释掉。
StringBuilder result = new StringBuilder();
try
{
ftp.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = ftp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
// to remove the trailing ‘\n‘
result.Remove(result.ToString().LastIndexOf(‘\n‘), 1);
reader.Close();
response.Close();
downloadFiles = result.ToString().Split(‘\n‘);
}
catch (Exception ex)
{
throw ex;
}
return downloadFiles;
}
/// <summary>
/// 获取当前目录下所有的文件夹列表(仅文件夹)
/// </summary>
/// <returns></returns>
public string[] GetDirectoryList()
{
string[] drectory = GetFilesDetailList();
string m = string.Empty;
foreach (string str in drectory)
{
int dirPos = str.IndexOf("<DIR>");
if (dirPos>0)
{
/*判断 Windows 风格*/
m += str.Substring(dirPos + 5).Trim() + "\n";
}
else if (str.Trim().Substring(0, 1).ToUpper() == "D")
{
/*判断 Unix 风格*/
string dir = str.Substring(54).Trim();
if (dir != "." && dir != "..")
{
m += dir + "\n";
}
}
}
char[] n = new char[] { ‘\n‘ };
return m.Split(n);
}
/// <summary>
/// 判断当前目录下指定的子目录是否存在
/// </summary>
/// <param>指定的目录名</param>
public bool DirectoryExist(string RemoteDirectoryName)
{
string[] dirList = GetDirectoryList();
foreach (string str in dirList)
{
if (str.Trim() == RemoteDirectoryName.Trim())
{
return true;
}
}
return false;
}
/// <summary>
/// 判断当前目录下指定的文件是否存在
/// </summary>
/// <param>远程文件名</param>
public bool FileExist(string RemoteFileName)
{
string[] fileList = GetFileList("*.*");
foreach (string str in fileList)
{
if (str.Trim() == RemoteFileName.Trim())
{
return true;
}
}
return false;
}
/// <summary>
/// 创建文件夹
/// </summary>
/// <param></param>
public void MakeDir(string dirName)
{
FtpWebRequest reqFTP;
try
{
// dirName = name of the directory to create.
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "MakeDir Error --> " + ex.Message);
}
}
/// <summary>
/// 获取指定文件大小
/// </summary>
/// <param></param>
/// <returns></returns>
public long GetFileSize(string filename)
{
FtpWebRequest reqFTP;
long fileSize = 0;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength;
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileSize Error --> " + ex.Message);
}
return fileSize;
}
/// <summary>
/// 改名
/// </summary>
/// <param></param>
/// <param></param>
public void ReName(string currentFilename, string newFilename)
{
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + currentFilename));
reqFTP.Method = WebRequestMethods.Ftp.Rename;
reqFTP.RenameTo = newFilename;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "ReName Error --> " + ex.Message);
}
}
/// <summary>
/// 移动文件
/// </summary>
/// <param></param>
/// <param></param>
public void MovieFile(string currentFilename, string newDirectory)
{
ReName(currentFilename, newDirectory);
}
/// <summary>
/// 切换当前目录
/// </summary>
/// <param></param>
/// <param>true 绝对路径 false 相对路径</param>
public void GotoDirectory(string DirectoryName, bool IsRoot)
{
if (IsRoot)
{
ftpRemotePath = DirectoryName;
}
else
{
ftpRemotePath += DirectoryName + "http://www.mamicode.com/";
}
ftpURI = "ftp://" + ftpServerIP + "http://www.mamicode.com/" + ftpRemotePath + "http://www.mamicode.com/";
}
/// <summary>
/// 删除订单目录
/// </summary>
/// <param>FTP 主机地址</param>
/// <param>FTP 用户名</param>
/// <param>FTP 用户名</param>
/// <param>FTP 密码</param>