FTP使用心得

1、创建文件夹的函数,一次只能创建一层。

2、没有现成的判断文件夹是否存在的函数,如果文件夹不存在就创建,会报异常。有以下封装好的函数。可以直接调用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// <summary>
/// 检测目录是否存在
/// </summary>
/// <param name="pFtpServerIP"></param>
/// <param name="pFtpUserID"></param>
/// <param name="pFtpPW"></param>
/// <returns>false不存在,true存在</returns>
public static bool DirectoryIsExist(Uri pFtpServerIP, string pFtpUserID, string pFtpPW)
{
    string[] value = GetFileList(pFtpServerIP, pFtpUserID, pFtpPW);
    if (value == null)
    {
        return false;
    }
    else
    {
        return true;
    }
}
public static string[] GetFileList(Uri pFtpServerIP, string pFtpUserID, string pFtpPW)
{
    StringBuilder result = new StringBuilder();
    try
    {
        FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(pFtpServerIP);
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(pFtpUserID, pFtpPW);
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
 
        WebResponse response = reqFTP.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string line = reader.ReadLine();
        while (line != null)
        {
            result.Append(line);
            result.Append(" ");
            line = reader.ReadLine();
        }
        reader.Close();
        response.Close();
        return result.ToString().Split(' ');
    }
    catch
    {
        return null;
    }
}

调用DirectoryIsExist函数即可,该函数是通过判断文件夹列表来实现的。

原文地址:https://www.cnblogs.com/Ebony-Ivory/p/4291219.html