piątek, 17 marca 2017

FTP Visual C++



#pragma endregion
Uri^ WWW_Ftp;
StreamReader^ SR;
FtpWebResponse^ date;
FtpWebRequest^ infoFiles;
Stream^ Str;
FileStream^ download_File;
System::Int32 size_Files;
array<Byte>^ size_Byte;

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
size_Byte=gcnew array<Byte>(2048);
}


private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
 listBox1->Items->Clear();
String^ text;
String^ text_Credential1="anonymous";
String^ text_Credential2="lukasz.wierzbicki2@gmail.com";
text=textBox1->Text;
WWW_Ftp=gcnew Uri("ftp://"+text);
infoFiles=dynamic_cast<FtpWebRequest^>(WebRequest::Create(WWW_Ftp));
infoFiles->Credentials=gcnew NetworkCredential(text_Credential1,text_Credential2);
infoFiles->Method=WebRequestMethods::Ftp::ListDirectoryDetails;
date=dynamic_cast<FtpWebResponse^>(infoFiles->GetResponse());
Str=date->GetResponseStream();
SR=gcnew StreamReader(Str);
do
{
listBox1->Items->Add(SR->ReadLine());
}while(!SR->EndOfStream);
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
Close();
}
private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
String^ text_Files;
String^ text_Credential1="anonymous";
String^ text_Credential2="lukasz.wierzbicki2@gmail.com";
text_Files="ftp://"+textBox1->Text+"/"+textBox2->Text;
WWW_Ftp=gcnew Uri(text_Files);
infoFiles=dynamic_cast<FtpWebRequest^>(WebRequest::Create(WWW_Ftp));
infoFiles->Credentials=gcnew NetworkCredential(text_Credential1,text_Credential2);
infoFiles->Method=WebRequestMethods::Ftp::DownloadFile;
date=dynamic_cast<FtpWebResponse^>(infoFiles->GetResponse());
Str=date->GetResponseStream();
download_File=gcnew FileStream("./"+textBox2->Text,FileMode::Create);
do
{
size_Files=Str->Read(size_Byte,0,size_Byte->Length);
download_File->Write(size_Byte,0,size_Files);
}while(size_Files!=0);
download_File->Flush();
download_File->Close();
date->Close();
Str->Close();


}
private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
textBox3->Text=listBox1->Text;
}
};
}

Brak komentarzy:

Prześlij komentarz