I am using a ftpwebrequest method to get a listing of all the files on the ftp directory. Then I use regex to extract the name of the file I need to download (matching on abc.com.ftc1.). After I have this file name I know exactly what file I need to download, so I use webclient DownloadData method to download the file I need to download.
After that I use webclient upload method to upload the same file to the second ftp site.
The problem I have is that webclient upload method throws an exception when I pass the variable that holds filepath to the upload method. If I assign the filepath to the variable manually from within code, then the upload works. Remember, I cannot hardcode the file name from within code as the filename changes everyday.
Let me paste some code and things will be more clear.
First I get a listing of all the file names on the ftp directory in a stream reader. I assign the contents of the stream reader to a string variable called myFiles. This part of the code works great.
//Gets names of all files on the ftp site
Expand|Select|Wrap|Line Numbers
- private void ListDirectory(string serverFolderURL, string ftpUser, string ftpPass)
- {
- NetworkCredential creds = new NetworkCredential(ftpUser, ftpPass);
- StreamReader strmReader = null;
- try
- {
- FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(serverFolderURL);
- listRequest.Credentials = creds;
- listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
- FtpWebResponse serverResponse = (FtpWebResponse)listRequest.GetResponse();
- strmReader = new StreamReader(serverResponse.GetResponseStream());
- myFiles = strmReader.ReadToEnd();
- }
- catch (System.Exception ex)
- {
- messageString += ex.Message.ToString();
- }
- if (strmReader != null)
- strmReader.Close();
- }
Since myFiles contains a string that has all the file names on the ftp site, I use regex to extract the name of the file that I need to download.
//Finds the ftc1 filename and sets it to the ftc1Filename variable
Expand|Select|Wrap|Line Numbers
- private void SetFtc1Filename(string myString)
- {
- string pattern = @"\abc.com.ftc1.\w*\b";
- Match myMatch = Regex.Match(myString, pattern);
- if (myMatch.Success)
- {
- ftc1Filename = myMatch.Value.ToString().Trim();
- }
- }
Now the variable ftc1Filename contains the name of the file I need to download. I use webclient download method to download the file I need to a local directory. This part of the code works great.
Expand|Select|Wrap|Line Numbers
- private void DownloadFile(string fileName)
- {
- WebClient request = new WebClient();
- request.Credentials = new NetworkCredential(downloadFtpUser, downloadFtpPass);
- try
- {
- byte[] fileData = request.DownloadData(downloadFtpAddress + "\\" + fileName);
- FileStream file = File.Create(localDownloadLoc + fileName);
- file.Write(fileData, 0, fileData.Length);
- file.Close();
- }
- catch (System.Exception ex)
- {
- messageString += ("\r\n" + ex.Message.ToString());
- }
- }
Now that I've downloaded the file I need, I need to upload it to the destination ftp site. I use web client upload method to do this.
Expand|Select|Wrap|Line Numbers
- private void Upload(string uploadFtpAddress, string uploadFtpUser, string uploadFtpPass, string filename)
- {
- using (System.Net.WebClient client = new System.Net.WebClient())
- {
- client.Credentials = new System.Net.NetworkCredential(uploadFtpUser, uploadFtpPass);
- client.UploadFile(uploadFtpAddress + "/" + new FileInfo(filename).Name, "STOR", filename);
- }
- }
The above method is the one that throws an exception: The remote server returned an error: (500) Syntax error, command unrecognized.
I am calling the webclient upload method with the following statements:
Expand|Select|Wrap|Line Numbers
- ftc1FilePath = localDownloadLoc + ftc1Filename;
- if (System.IO.File.Exists(ftc1FilePath))
- {
- try
- {
- Upload(uploadFtpAddress, uploadFtpUser, uploadFtpPass, ftc1FilePath);
- messageString += ("\r\n" + ftc1Filename + " " + "was successfully uploaded to" + " " + uploadFtpAddress);
- }
- catch (System.Exception ex)
- {
- messageString += ("\r\n" + ftc1Filename + " " + "could not be uploaded to" + " " + uploadFtpAddress + "." + " " + ex.Message.ToString());
- }
- }
The variable localDownloadLoc has been initialized with @"C:\FTC Move Files\" and ftc1FilePath already contains the name of the file (retrieved using regex).
Therefore, "ftc1FilePath = localDownloadLoc + ftc1Filename;" variable has to compete path for the file on the local directory that is to the uploaded.
Now here's the funny thing. If I replace ftc1Filename in the above statement with the actual name of the file, then the webclient upload method works, like this
ftc1FilePath = localDownloadLoc + "abc.com.ftc1.062210083741"; //This works
Stepping through the code, I can see that both "localDownloadLoc + "abc.com.ftc1.062210083741"" and "localDownloadLoc + ftc1Filename" contain exactly the same value, but the former works and the latter does not.
Any help with this will be highly appreciated. Sorry the post turned out to be so long, but I tried to provide as much info as possible.
Thanks.
RJ