locked
FTPWebRequest.RenameTo Error in .NET v4.0 RRS feed

  • Question

  • Hello,

    Ich have Method, which renames some file on FTP Servers. The Method is running well in
    .NET 2.0, 3.0, 3.5 but not in 4.0. I get always the Errorcode 550 from the FTP Server "No such File".

    Here's a sample of the code i am using:

                System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)WebRequest.Create(new Uri(new Uri(@"ftp://ftpserver.com/"), "file.xml"));
                request.Method = WebRequestMethods.Ftp.Rename;
                request.KeepAlive = false;
                request.UseBinary = false;
                request.RenameTo = "file.xml.tmp";
                request.Credentials = new NetworkCredential("uid""pwd");

                WebResponse response = request.GetResponse();

    As i wrote above, in .NET Versions 2.0 to 3.5 it's working well.

    Have anybody an Idea why it's not running in 4.0?

    regards

    Wednesday, July 28, 2010 3:24 PM

All replies

  • no ideas?
    Wednesday, August 4, 2010 10:19 AM
  • Hello,

    I have the same issue, we just recently upgraded to 4.0 and this started to missbehave.

    When I traced the communication to the FTP-server (see http://blogs.msdn.com/b/feroze_daud/archive/2005/05/12/416922.aspx) and reviewed the logs comparing the same code running with 3.5 and 4.0 versions of .NET I found the following:

     

    When using Framework 3.5  the rename-command was sent this way:

    System.Net Information: 0 : [9764] FtpControlStream#9453167 - Sending command [RNFR TMP_PRI.PRIT0010.0005020101129.DAT.tmp]

     

    When using Framework 4.0 the rename-command looked like this:

    System.Net Information: 0 : [4512] FtpControlStream#52986476 - Sending command [RNFR /TMP_PRI.PRIT0010.0005020101129.DAT.tmp]

     

    There is a small but perhaps significant difference in a "/" before the filename in the latter log, is this is a bug, does anyone know of a workaround?

    Regards,

    Christian Lindkvist

     

     

    Tuesday, November 2, 2010 8:55 AM
  • I can add that when you run the code against an FTP-server such as IIS it doesnt matter that the new implementation sends an extra "/" in the request, it works either way.

    I am not an expert in FTP so it might be that I couldnt replicate the way things were set up on our production-server (which is located externally so I dont even know what brand it is), but this could indicate that this problem only exist with a few FTP-servers.

     

    Edited and added workaround:

    I found a workaround to my problem, if you add the prefix "%2E/" to the filename, that is if your filename is "file.xml" make it "%2E/filename.xml". This works for the FTP-server we had the problem with.

    I have submitted the problem stated above as a bug in Framework 4.0 to Microsoft, we´ll see if they agree that its a bug and not a feature :).

     


    Regards Christian Lindkvist
    Tuesday, November 2, 2010 12:12 PM
  • I am having this exact problem listed in this thread. I never actually had it working or needed renaming an FTP file until .NET 4.0 so that was the 1st code I wrote.  I always get back: "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."

    Guess what... copied the exact code into an old VS.NET 2008 test project ran it again -> worked perfectly!

    So I thought I would just wrap this code up in a binary targeting the 3.5 Framework and then reference it in my VS.NET 2010 project to outsmart the bug, but it did not work.

    My trace log kept outputting the same results:

    System.Net Information: 0 : [2228] FtpControlStream#15409429 - Received response [257 "/users/company" is current directory]
    System.Net Information: 0 : [2228] FtpControlStream#15409429 - Sending command [RNFR /myfile.txt]
    System.Net Information: 0 : [2228] FtpControlStream#15409429 - Received response [550 File /myfile.txt not found]

    VS.NET 2008 has different results ommiting the 1st slash before the rename file:

    System.Net Information: 0 : [6460] FtpControlStream#40715158 - Received response [257 "/users/company" is current directory]
    System.Net Information: 0 : [6460] FtpControlStream#40715158 - Sending command [CWD /users/company/]
    System.Net Information: 0 : [6460] FtpControlStream#40715158 - Received response [250 Command CWD succeed]
    System.Net Information: 0 : [6460] FtpControlStream#40715158 - Sending command [RNFR myfile.txt]
    System.Net Information: 0 : [6460] FtpControlStream#40715158 - Received response [350 Enter new name]
    System.Net Information: 0 : [6460] FtpControlStream#40715158 - Sending command [RNTO myfileOLD.txt]
    System.Net Information: 0 : [6460] FtpControlStream#40715158 - Received response [250 Renamed]

    I have tried every combination and can't get this working, except when running in VS.NET 2008 or prior. This does sound like a bug or an undocumented procedure change that probably warrants an entry on connect.microsoft.com.

    Any help or suggestions on this?

    • Edited by atconway Friday, November 12, 2010 8:17 PM
    Thursday, November 11, 2010 10:08 PM
  • I just added a bug report with Microsoft Connect on this issue:


    If it is not a bug, then at least the MSDN documentation on the 'FtpWebRequest' class (file renaming specifically) needs to be updated with documentation on how to handle the way the FTP commands are being issued under the covers.

    I implemented the escape character @Christian reccomended, but rather in the request URI and not on the 'RenameTo' property, and I have a work around. As you could see in the trace from the VS.NET 2008 version, it also issues a change directory command: "CWD /users/company/" that is not issued in VS.NET 2010 version. The undocumented (at least from what I read) but workable solution is to use the /%2f escape characters (as suggested earlier) in the request URI ONLY between the folders that are resolved to as the current directory when logging onto the server. Essentially I had to be explicit and give an absolute path, using these special escape characters only between the default folders locations. The new URL request line of code is as follows below:

    Dim Request As FtpWebRequest = DirectCast(WebRequest.Create(New Uri("ftp://www.mycompany.com/%2fusers/%2fcompany/myfile.txt")), FtpWebRequest) 

    I believe this is probably only a problem that will occur for those of you that log onto FTP servers, where you are resolved to a default directory automatically, and may not even provide that directory in the request.
    • Proposed as answer by atconway Friday, November 12, 2010 8:29 PM
    Friday, November 12, 2010 8:29 PM
  • Maybe the issue is solved, but it still didn't work work in my case. Neither did the above described work arounds. Here you can find the solution or work around that worked for me:

    http://stackoverflow.com/questions/4159903/problem-renaming-file-on-ftp-server-in-net-framework-4-0-only/5897531#5897531

     

    Thursday, May 5, 2011 5:23 PM
  • The use of Url changed between version IIRC. From the original version the class did a CD as the first command and I believe that changed.

    Can you take a trace of the connection on both versions to see what the difference is. See http://msdn.microsoft.com/en-us/library/ty48b824.aspx


    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.

    Tuesday, September 4, 2012 8:24 PM