I have for example a ftp server with the address ftp.mywebsite.com
On this ftp server I have a path: FileFolder\\file1.txt

What I wonder is how it would be possible to connect to this ftp server and read/write to that file.
Is there any good articles of how this is done etc... It is a new area for me.

Would be glad for advice..

This was a good guide, I am reading it right now but it is very much to read and not really a practical example where you can follow the whole procedure, it is "just" fragments of pieces for different functions.
It is difficult to get the right picture.

I have also found something that declares this. I wonder if that could be a start. In some way, I must need some kind of

::hostname,
::username,
::password

to the ftp server etc...

The goal is to read/write to the file "Folder1\\File1.txt" on the "ftp.myaddress.com".

I dont really know if this line is a start for something:

using namespace System::Net::Sockets;

 TcpClient^ mtTCPClient = gcnew TcpClient();

 mtTCPClient->Connect( "ftp.myaddress.com", 25 );
// Request to write						 
Stream^ newStream = objRequest->GetRequestStream();



//Write something to file for test
StreamWriter^ writer = gcnew StreamWriter(newStream);

for( int i = 0; i < 3; i++ ) 
{
	 writer->WriteLine("Hello");
}
writer->Close();
objResponse->Close(); //Closes the connection to the server;

I have succeded to Connect to my ftp.server and read the lines from the file(Code below)

Next step is to just "overwrite" the file with new lines(Above Code)
I have tried out the code above but when running that code, I get the error:

"Cannot send a content-body with this verb-type."

Wonder what I am missing ?


(Complete Code Below)

//First a request to FTP Server shall be initiated, requesting the file needed. 
String^ FilePath = "ftp://onewebsite.com:21/fileFolder/file1.txt";

//Create just "Creates" a new WebRequest for that path, then comes the actions:
FtpWebRequest^ objRequest = (FtpWebRequest^)WebRequest::Create(FilePath);
objRequest->Credentials = gcnew NetworkCredential("username", "password");			 



//Then, we have to get the response for this request:
FtpWebResponse^ objResponse = (FtpWebResponse^) objRequest->GetResponse();
			 
//Get the stream of file:
Stream^ responseStream = objResponse->GetResponseStream();




//read the file;
List<String^> Lines = gcnew List<String^>();
StreamReader^ reader = gcnew StreamReader(responseStream);

 while( reader->Peek() >= 0 )
 {
	 Lines.Add(reader->ReadLine()); //Save all Lines in the field;
 }
 //Close
 reader->Close();
 responseStream->Close(); //close the stream after reading;





// Request to write						 
Stream^ newStream = objRequest->GetRequestStream();



//Write something to file for test
StreamWriter^ writer = gcnew StreamWriter(newStream);

for( int i = 0; i < 3; i++ ) 
{
	 writer->WriteLine("Hello");
}
writer->Close();
objResponse->Close(); //Closes the connection to the server;

I'm only a Noob but you should only need to save the file as it will overwrite it's self.

You're not saving the file, just closing the connection.

That seems interesting. I wonder how that is possible to do in this case.
Should I use UploadFile to overwrite that file on the server. I don´t know how the whole procedure will go for this ?

objRequest->Method = WebRequestMethods::Ftp::UploadFile;

I'm only a Noob but you should only need to save the file as it will overwrite it's self.

You're not saving the file, just closing the connection.

This article has been dead for over six months. Start a new discussion instead.