C# Upload whole directory using FTP

后端 未结 4 993
再見小時候
再見小時候 2021-01-04 20:50

What I\'m trying to do is to upload a website using FTP in C# (C Sharp). So I need to upload all files and folders within a folder, keeping its structure. I\'m using this FT

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-04 21:15

    Problem Solved! :)

    Alright so I managed to write the method myslef. If anyone need it feel free to copy:

    private void recursiveDirectory(string dirPath, string uploadPath)
        {
            string[] files = Directory.GetFiles(dirPath, "*.*");
            string[] subDirs = Directory.GetDirectories(dirPath);
    
            foreach (string file in files)
            {
                ftpClient.upload(uploadPath + "/" + Path.GetFileName(file), file);
            }
    
            foreach (string subDir in subDirs)
            {
                ftpClient.createDirectory(uploadPath + "/" + Path.GetFileName(subDir));
                recursiveDirectory(subDir, uploadPath + "/" + Path.GetFileName(subDir));
            }
        }
    

    It works very well :)

提交回复
热议问题