FtpWebRequest and (550) File unavailable (e.g., file not found, no access)

Let’s just give you the punchline of the post first:

If you’re trying to access the file “ftp://somehost/somedirectory/some.filename” and you’re getting a 550 error, you need to change the Uri to “ftp://somehost/%2f/somedirectory/some.filename”

You can find more details on Mariya Atanasova’s Blog.  OK, now that I’ve performed the public service, let’s explain why this works. 

FtpWebRequest interprets every directory in the chain as a “CWD” command (CD in DOS terminology).  Each of these is relative to the previous location.  The first location is wherever the FTP server dumped you on logon.  A behaviour that is irritating in interactive mode is made just plain unusable as an API.  What’s worse, Microsoft have actually implemented the spec correctly (section 3.2.2 if you really care).  This basically means that FTP urls don’t work the way you expect and don’t behave in a similar way to HTTP urls.

So, to fix this, we need to first change to the root directory.  That, of course, means executing a “CWD /” command.  Of course, since / is a special character in the URL syntax, you end up having to write “%2F” to trick the FtpWebRequest into doing the right thing.  Ultimately, the moral of this story is that FtpWebRequest and ftp URIs are the wrong model for interacting with FTP.  I can’t see that being changed anytime soon, however.

Published by

Julian Birch

Full time dad, does a bit of coding on the side.

One thought on “FtpWebRequest and (550) File unavailable (e.g., file not found, no access)”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s