我正在尝试学习有关通过 ftp 连接上传到另一台主机的新知识。我知道如何上传单个文件。但是如果我想上传完整的文件夹及其上存在的完整子文件夹和文件怎么办?
这是我的一个文件上传
private void Form1_Load(object sender, EventArgs e)
{
Upload("Test.txt");
}
public void Upload(string fileToUpload)
{
try
{
FileInfo toUpload = new FileInfo(fileToUpload);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/" + toUpload.Name);
MessageBox.Show(WebRequestMethods.Ftp.ListDirectory);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("Uploader", "3635451");
Stream ftpStream = request.GetRequestStream();
FileStream file = File.OpenRead(fileToUpload);
int length = 1024;
byte[] buffer = new byte[length];
int bytesRead = 0;
do
{
bytesRead = file.Read(buffer, 0, length);
ftpStream.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
file.Close();
ftpStream.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我在谷歌上搜索了很多关于上传文件夹的信息,但我只是失败了,提前谢谢你。