C# 如何检查特定子目录是否已存在?

C# 如何检查特定子目录是否已存在?,c#,.net,winforms,ftp,C#,.net,Winforms,Ftp,我现在使用的代码是: string[] Files = GetFileList(); ArrayList arrDirectories = new ArrayList(); if (Files != null) { foreach (string dir in Files) { arrDirectories.Add(dir); } } if (!arrDirectories.Contains(dirName)) { Sync(dirName, reqFTP, respon

我现在使用的代码是:

string[] Files = GetFileList();
ArrayList arrDirectories = new ArrayList();
if (Files != null)
{
  foreach (string dir in Files)
  {
    arrDirectories.Add(dir);
  }
}
if (!arrDirectories.Contains(dirName))
{
  Sync(dirName, reqFTP, response, ftpStream);
}
方法GetFileList:

public string[] GetFileList()
{
  string[] downloadFiles;
  StringBuilder result = new StringBuilder();
  WebResponse response = null;
  StreamReader reader = null;
  try
  {
    FtpWebRequest reqFTP;
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + f.Host + "/"));
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential(f.Username, f.Password);
    reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
    reqFTP.Proxy = null;
    reqFTP.KeepAlive = false;
    reqFTP.UsePassive = false;
    response = reqFTP.GetResponse();
    reader = new StreamReader(response.GetResponseStream());
    string line = reader.ReadLine();
    while (line != null)
    {
      result.Append(line);
      result.Append("\n");
      line = reader.ReadLine();
    }
    result.Remove(result.ToString().LastIndexOf('\n'), 1);
    return result.ToString().Split('\n');
   }
   catch (Exception ex)
   {
     if (reader != null)
     {
       reader.Close();
     }
     if (response != null)
     {
       response.Close();
     }
     downloadFiles = null;
     return downloadFiles;
    }
 }
我最终得到的是来自ftp服务器的所有文件、目录和子目录。问题是,每个文件、目录和子目录都在自己的索引中。 例如,我在ARR目录中的索引0中看到:test 然后在索引1中我看到:testsub 然后在索引2中我看到:test.jpg

但实际上test在根目录下,testsub在test下,test.jpg在testsub下。 问题是,当我检查目录是否已经存在时:

if (!arrDirectories.Contains(dirName))
例如,如果它是一个单独的目录测试,那么就没有问题。 但是如果我想检查exist的目录是一个子目录,比如test/testsub 然后它将永远找不到它,即使它存在于我的ftp服务器上

因此,如果我必须检查test/testsub或test/test1/test2/test3之类的目录,那么在GEtFileList方法中首先应该更改什么?那么,如何在ARR目录上循环可能需要一个递归

尝试使用此代码' 首先将目录结构放入字符串数组:

string[]Directories=Directory.GetDirectories(@“c:\windows\Temp”、“***”、SearchOption.AllDirectories);

是的,你说得对,最简单的方法是递归调用GetFileList() 并将结果放入列表中,而不是字符串中 这是你修改过的代码

    static public void GetFileList(List<string> list,string sroot)
    {

        WebResponse response = null;
        StreamReader reader = null;
        try
        {
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + Host+sroot));
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(Username, Password);
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails ;
            reqFTP.Proxy = null;
            reqFTP.KeepAlive = false;
            reqFTP.UsePassive = false;
            response = reqFTP.GetResponse();
            reader = new StreamReader(response.GetResponseStream());
            string line = reader.ReadLine();
            string file;
            while (!reader.EndOfStream ) 
            {
                if (line.StartsWith("-"))
                {
                    file = sroot + "/" + line.Substring(57);
                    list.Add(file);
                }
                else if (line.StartsWith("d"))
                {
                    file = sroot + "/" + line.Substring(57);
                    list.Add(file+"/");
                    GetFileList(list, file);
                }
                line = reader.ReadLine();
            }
        }
        catch (Exception ex)
        {
            if (reader != null)
            {
                reader.Close();
            }
            if (response != null)
            {
                response.Close();
            }
        }
        return;
    }
static public void GetFileList(列表列表,字符串sroot)
{
WebResponse=null;
StreamReader=null;
尝试
{
ftpWebRequestReqftp;
reqFTP=(FtpWebRequest)FtpWebRequest.Create(新Uri(“ftp://“+Host+sroot));
reqFTP.useBarbinary=true;
reqFTP.Credentials=新的网络凭据(用户名、密码);
reqFTP.Method=WebRequestMethods.Ftp.ListDirectoryDetails;
reqFTP.Proxy=null;
reqFTP.KeepAlive=false;
reqFTP.UsePassive=false;
response=reqFTP.GetResponse();
reader=newstreamreader(response.GetResponseStream());
字符串行=reader.ReadLine();
字符串文件;
而(!reader.EndOfStream)
{
if(带(“-”)的行开始)
{
file=sroot+“/”+line.Substring(57);
列表。添加(文件);
}
else if(第行开始,以“d”开头)
{
file=sroot+“/”+line.Substring(57);
添加(文件+“/”);
GetFileList(列表、文件);
}
line=reader.ReadLine();
}
}
捕获(例外情况除外)
{
if(读卡器!=null)
{
reader.Close();
}
if(响应!=null)
{
response.Close();
}
}
返回;
}
  • 调用方法ListDirectoryDetails而不是ListDirectory来 区分目录和文件
  • 解析结果以从文件详细信息中获取文件名(我不确定它始终是从位置57:-)开始的行的末尾)
  • 为每个找到的文件夹调用self
  • 流结束的条件是EndOfStream:-)
这是电话

List<string> filelist = new List<string>();
GetFileList(filelist, "");
foreach (string s in filelist )
    Console.WriteLine(s.Substring(1)); 
List filelist=newlist();
GetFileList(文件列表“”);
foreach(文件列表中的字符串s)
控制台写入线(s.子字符串(1));
所有文件夹的末尾都有“/”(请不要将文件夹包括在列表中)。
使用前面答案中的方法很容易建立本地文件列表

不要构建字符串,只会在以后拆分它。而是将得到的行添加到结果列表中