Hi All,
I'm building a service that regularly backs up a set of files via FTP, and am having some problems. I have a class (from: http://dotnetperls.com/recursively-find-files) that recursively builds a listing of the source directory and subdirectories. Then I upload using an FtpWebRequest. The upload works fine for the individual files in the root directory, but dies with a "550 - File Unavailable" error when it gets to the subdirectories.
I'm writing the path out to file as a means of debugging and it looks to be ok to me, so I'm not sure what's going on. My Ftp code follows. Thanks in advance.
I'm building a service that regularly backs up a set of files via FTP, and am having some problems. I have a class (from: http://dotnetperls.com/recursively-find-files) that recursively builds a listing of the source directory and subdirectories. Then I upload using an FtpWebRequest. The upload works fine for the individual files in the root directory, but dies with a "550 - File Unavailable" error when it gets to the subdirectories.
I'm writing the path out to file as a means of debugging and it looks to be ok to me, so I'm not sure what's going on. My Ftp code follows. Thanks in advance.
|

veggieCoderPosted Aug 27, 2010, 1:01 PM
I've finally got this one worked out. I was calling my recursive function in the wrong place, and effectively overwriting the directory structure I was building. Here is the completed, working code which I hope will help someone else who might be struggling with a similar problem.
buildDirectories(whichdir);
List
foreach (string p in dirs)
{
StringBuilder builder2 = new StringBuilder(p);
builder2.Remove(0, 38);
builder2.Replace('\\', '/');
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri("ftp://
request.Timeout = 10000;
request.ReadWriteTimeout = 10000;
request.UseBinary = true;
request.KeepAlive = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
FileInfo uploadFile = new FileInfo(p);
request.ContentLength = uploadFile.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contLength;
FileStream fStream = uploadFile.OpenRead();
Stream requestWriter = request.GetRequestStream();
contLength = fStream.Read(buff, 0, buffLength);
while (contLength != 0)
{
requestWriter.Write(buff, 0, buffLength);
contLength = fStream.Read(buff, 0, buffLength);
}
requestWriter.Close();
fStream.Close();
}
}
public static void buildDirectories(String whichDir)
{
DirectoryInfo dirInfo = new DirectoryInfo(whichDir);
foreach (DirectoryInfo d in dirInfo.GetDirectories())
{
StringBuilder builder = new StringBuilder(d.FullName.ToString());
builder.Remove(0, 38);
builder.Replace('\\', '/');
FtpWebRequest createDirs = WebRequest.Create("ftp://
createDirs.Method = WebRequestMethods.Ftp.MakeDirectory;
createDirs.KeepAlive = false;
createDirs.UseBinary = true;
using (var response = (FtpWebResponse)createDirs.GetResponse())
{
writer.WriteLine(response.StatusCode);
response.Close();
}
buildDirectories(d.FullName.ToString());
}
}
Sam HobbsPosted Aug 26, 2010, 1:06 PM
veggieCoderPosted Aug 26, 2010, 12:39 PM
I'm currently working on a recursive function to build the entire directory structure on the server before uploading any files, and am running into problems with that. It will build the first directory then die with the same 550 error originally reported. I'm writing the paths that the buildDirectories() function creates out to file for debugging, and it is correctly capturing the directories and all subdirectories. I'd be grateful for any feedback. Here is my code:
public static void buildDirectories(String whichDir)
{
DirectoryInfo dirInfo = new DirectoryInfo(whichDir);
foreach (DirectoryInfo d in dirInfo.GetDirectories())
{
StringBuilder builder = new StringBuilder(d.FullName.ToString());
builder.Remove(0, 38);
builder.Replace('\\', '/');
writer.WriteLine(builder);
FtpWebRequest createDirs = WebRequest.Create("ftp://
createDirs.Method = WebRequestMethods.Ftp.MakeDirectory;
createDirs.KeepAlive = true;
createDirs.UseBinary = true;
buildDirectories(d.FullName.ToString());
using (var response = (FtpWebResponse)createDirs.GetResponse())
{
writer.WriteLine(response.StatusCode);
}
}
}
Sam HobbsPosted Aug 20, 2010, 3:49 PM