Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ftp in C# to send a file

Tags:

c#

.net

ftp

I'm trying to send a file using ftp. I have the following code:

string server = "x.x.x.x";  // Just the IP Address 

FileStream stream = File.OpenRead(filename);
byte[] buffer = new byte[stream.Length];

WebRequest request = WebRequest.Create("ftp://" + server);
request.Method = WebRequestMethods.Ftp.UploadFile;            
request.Credentials = new NetworkCredential(username, password);

Stream reqStream = request.GetRequestStream(); // This line fails
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();

But when I run it, I get the following error:

The requested URI is invalid for this FTP command.

Please can anyone tell me why? Am I using this incorrectly?

like image 239
Paul Michaels Avatar asked May 05 '10 11:05

Paul Michaels


People also ask

What is FTP in C?

The File Transfer Protocol (FTP) is a standard communication protocol used for the transfer of computer files from a server to a client on a computer network. FTP is built on a client–server model architecture using separate control and data connections between the client and the server.

Can we do socket programming in C?

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.

What is socket () in C?

The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The socket() function takes the following arguments: domain. Specifies the communications domain in which a socket is to be created. type.

How do you implement FTP?

On the taskbar, click Start, and then click Control Panel. In Control Panel, click Programs and Features, and then click Turn Windows Features on or off. Expand Internet Information Services, and then FTP Server. Select FTP Service.


1 Answers

I think you need to specify the path and filename you're uploading too, so I think it should be either of:

WebRequest request = WebRequest.Create("ftp://" + server + "/");

WebRequest request = WebRequest.Create("ftp://" + server + "/filename.ext");
like image 142
Hans Olsson Avatar answered Sep 24 '22 22:09

Hans Olsson