将文件从一个目录移动到另一个目录的问题 [英] Issue with moving a file from one directory to another
问题描述
我有一些程序需要将文件从一个目录移动到FTP服务器上的另一个目录中(ftp://10.3.4.5.1/ftpfolder/folder \ test.txt,ftp://10.3.4.5.1/ftpfolder/backup \ test.txt)
Hi,
I have program that need to move a file one directry to another directory on a FTP server Example (ftp://10.3.4.5.1/ftpfolder/folder\test.txt,ftp://10.3.4.5.1/ftpfolder/backup\test.txt)
//This is the statement the OP really has:
//MoveFile("ftp://10.3.4.5.1", "anonymous", "", "/ftpfolder/", "/ftpfolder/backup\\", "test.txt");
//Editor changed to this because the code block formatting doesn't like \\"
MoveFile("ftp://10.3.4.5.1", "anonymous", "", "/ftpfolder/", < See Comment Above >, "test.txt");
public static string MoveFile(string ftpuri, string username, string password, string ftpfrompath, string ftptopath, string filename)
{
string retval = string.Empty;
//FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpuri + ftpfrompath + filename);
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://10.14.21.80/ftpfolder/test.txt");
ftp.Method = WebRequestMethods.Ftp.Rename;
ftp.Credentials = new NetworkCredential(username, password);
ftp.UsePassive = true;
//Statment OP really had was this:
//ftp.RenameTo = GetRelativePath("/ftpfolder/test.txt", "/Backup\\") + filename;
//Editor changed to this because the code block formatting doesn't like \\"
ftp.RenameTo = GetRelativePath("/ftpfolder/test.txt", < See Comment Above >) + filename;
Stream requestStream = ftp.GetRequestStream();
FtpWebResponse ftpresponse = (FtpWebResponse)ftp.GetResponse();
Stream responseStream = ftpresponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
return reader.ReadToEnd();
}
public static string GetRelativePath(string ftpBasePath, string ftpToPath)
{
if (!ftpBasePath.StartsWith("/"))
{
throw new Exception("Base path is not absolute");
}
else
{
ftpBasePath = ftpBasePath.Substring(1);
}
if (ftpBasePath.EndsWith("/"))
{
ftpBasePath = ftpBasePath.Substring(0, ftpBasePath.Length - 1);
}
if (!ftpToPath.StartsWith("/"))
{
throw new Exception("Base path is not absolute");
}
else
{
ftpToPath = ftpToPath.Substring(1);
}
if (ftpToPath.EndsWith("/"))
{
ftpToPath = ftpToPath.Substring(0, ftpToPath.Length - 1);
}
string[] arrBasePath = ftpBasePath.Split("/".ToCharArray());
string[] arrToPath = ftpToPath.Split("/".ToCharArray());
int basePathCount = arrBasePath.Count();
int levelChanged = basePathCount;
for (int iIndex = 0; iIndex < basePathCount; iIndex++)
{
if (arrToPath.Count() > iIndex)
{
if (arrBasePath[iIndex] != arrToPath[iIndex])
{ levelChanged = iIndex;
break;
}
}
}
int HowManyBack = basePathCount - levelChanged;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < HowManyBack; i++)
{
if (i == 0)
{
sb.Append("ftp://10.3.4.5.1/ftpfolder/");
}
} for (int i = levelChanged; i < arrToPath.Count(); i++)
{
sb.Append(arrToPath[i]); //sb.Append("/");
}
return sb.ToString();
}
错误:无法发送具有此动词类型的内容主体.
请任何人在这个问题上帮助我.我正在为此问题编写代码.谢谢.
[第二个编辑-注释掉了一些代码,因为CP上的代码格式不能正确处理\\,并且整个代码块的格式都被抛出.]
Errors : Cannot send a content-body with this verb-type.
Please any one help me on this issue. I am struggling to write code on this isssue. Thank you.
[Second Edit - Commented out some code because code formatting on CP doesn''t handle \\" quite right, and formatting was thrown off for the whole code block.]
推荐答案
你好
检查一下: http://social.msdn. microsoft.com/forums/zh-CN/netfxbcl/thread/f872bed1-8cd1-456b-a826-ff8c326f0d41 [
Hello
Check this out : http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/f872bed1-8cd1-456b-a826-ff8c326f0d41[^]
这篇关于将文件从一个目录移动到另一个目录的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!