如何在FTP服务器上将文件从一个目录移动到另一个目录 [英] How to move file from one directory to another on FTP server
本文介绍了如何在FTP服务器上将文件从一个目录移动到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我创建了一个Windows .net服务。用于将文件从(uri / fiolder1 / filename)移动到ftp的(uri / fiolder1 / Archive / filename)。我的代码如下:
Hi There,
I have created a windows .net service. which is used to move the files from (uri/fiolder1/filename) to (uri/fiolder1/Archive/filename) of ftp. My code is as follows:
FtpWebRequest Wr = null;
NetworkCredential User = null;
try
{
WriteLog("Processing Archiving on FTP: File Name: " + File);
User = new NetworkCredential(FTPUser, FTPPass);
Wr = (FtpWebRequest)FtpWebRequest.Create(FTPUri + File);
Wr.UseBinary = true;
Wr.KeepAlive = false;
Wr.Method = WebRequestMethods.Ftp.Rename;
Wr.Credentials = User;
//Wr.RenameTo = "Archive/" + DateTime.Now.AddHours(-11).ToString("dd-MM-yyyy_HH_mm_ss") +"_" +File;
Wr.RenameTo = "Archive/" + DateTime.Now.ToString("dd-MM-yyyy_HH_mm_ss") + "_" + File;
FtpWebResponse back = (FtpWebResponse)Wr.GetResponse();
//bool Success = back.StatusCode == FtpStatusCode.CommandOK || back.StatusCode == FtpStatusCode.FileActionOK;
back.Close();
Wr = null;
User = null;
return true;
}
catch (Exception ex)
{
WriteLog("Err at MoveFileToArchiveOnFTP :>" + ex.Message);
return false;
}
工作正常但有时会抛出错误:远程服务器返回错误:(450)文件不可用(例如,文件繁忙) 。
所以请尽快帮我解决这个错误。
It is working fine but some time it throws an error:The remote server returned an error: (450) File unavailable (e.g., file busy).
So please help me to resolve this error ASAP.
推荐答案
//strFTPIP is address of FTP Server and strFile is Filename
var ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(strFTPIP + strFile);
ftpWebRequest.KeepAlive = false;
ftpWebRequest.UseBinary = true;
//strFTPuserName is username of ftp server and strFTPPassword is password of ftp server
ftpWebRequest.Credentials = new NetworkCredential(strFTPuserName, strFTPPassword);
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
//filepath is path of the file in local server
using (var inputStream = File.OpenRead(filepath))
using (var outputStream = ftpWebRequest.GetRequestStream())
{
var buffer = new byte[4096];
int totalReadBytesCount = 0;
int readBytesCount;
while ((readBytesCount = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, readBytesCount);
totalReadBytesCount += readBytesCount;
var progress = totalReadBytesCount * 100.0 / inputStream.Length;
}
}
ftpWebRequest.Abort();
这篇关于如何在FTP服务器上将文件从一个目录移动到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文