Hello everyone,
Remark: due to spam prevention mechanizm I was forced to replace the beginning of the Uris from ftp:// to ftp.
I've got following problem. I have to upload file with C# ftp method and afterwards rename it. Easy, right? :)
Ok, let's say my ftp host is like this:
ftp.contoso.com
and after logging in, current directory is set to:
users/name
So, what I'm trying to achieve is to log in, upload file to current directory as file.ext.tmp and after upload is successful, rename the file to file.ext
The whole difficulty is, as I guess, to properly set the request Uri for FtpWebRequest.
MSDN states:
The URI may be relative or absolute. If the URI is of the form "ftp://contoso.com/%2fpath" (%2f is an escaped '/'), then the URI is absolute, and the current directory is /path. If, however, the URI is of the form "ftp://contoso.com/path", first the .NET Framework logs into the FTP server (using the user name and password set by the Credentials property), then the current directory is set to UserLoginDirectory/path.
Ok, so I upload file with the following URI:
ftp.contoso.com/file.ext.tmp
Great, the file lands where I wanted it to be: in directory "users/name"
Now, I want to rename the file, so I create web request with following Uri:
ftp.contoso.com/file.ext.tmp
and specify rename to parameter as:
file.ext
and this gives me 550 error: file not found, no permissions, etc.
I traced this in Microsoft Network Monitor and it gave me:
Command: RNFR, Rename from
CommandParameter: /file.ext.tmp
Ftp: Response to Port 53724, '550 File /file.ext.tmp not found'
as if it was looking for the file in the root directory - not in the current directory.
I renamed the file manually using Total Commander and the only difference was that CommandParameter was without the first slash:
CommandParameter: file.ext.tmp
I'm able to successfully rename the file by supplying following absolute URI:
ftp.contoso.com/%2fusers/%2fname/file.ext.tmp
but I don't like this approach, since I would have to know the name of current user's directory. It can probably be done by using WebRequestMethods.Ftp.PrintWorkingDirectory, but it adds extra complexity (calling this method to retrieve directory name, then combining the paths to form proper URI).
What I don't understand is why the URI ftp.contoso.com/file.ext.tmp is good for upload and not for rename? Am I missing something here?
The project is set to .NET 4.0, coded in Visual Studio 2010.