Tuesday, December 27, 2016

How to download FTP files using .NET Interop

download-1459071_1920_thumb

Recently i have worked on a project where the requirement was to upload and download the files from a FTP.  In this blog i will be explaining how to download the files using .NET Interop. There are several other blogs which have explained, how to download the files using ScriptingHost or .net interop but i have not found an example to download all the files from a particular folder.

These are couple of blogs i found, related to this.

http://www.archerpoint.com/blog/Posts/automating-command-line-functions-nav-rtc-transmit-ftp

http://www.dynamics.is/?p=583

Below is the code that downloads all the files from a particular FTP Folder to your local download folder. The GetFTPSetup is just another function to retrieve the setup values.

I have used two functions to download the files, the first function DownloadFilesFromFTP will find all the files and add to the list, then we will loop through the list to download the file using the second function.


PROCEDURE DownloadFilesFromFTP@1240060028();
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
       StreamReader@1240060010 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader";
       List@1240060012 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Generic.List`1";
       i@1240060011 : Integer;
       PathHelper@1240060013 : DotNet "'mscorlib'.System.IO.Path";
       FileAttributes@1240060014 : DotNet "'mscorlib'.System.IO.FileAttributes";
       DotNetFile@1240060015 : DotNet "'mscorlib'.System.IO.File";
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Download FilePath");
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'NLST';
       FTPWebResponse := FTPWebRequest.GetResponse();
       StreamReader := StreamReader.StreamReader(FTPWebResponse.GetResponseStream());
       List := List.List();
       FileName := StreamReader.ReadLine();
       WHILE FileName <> '' DO BEGIN
         List.Add(FileName);
         FileName := StreamReader.ReadLine();
       END;
       FOR i:=1 TO List.Count DO BEGIN

        IF FORMAT(List.Item(i-1)) <> 'archive' THEN BEGIN
           DownloadFileFromFTP(FORMAT(List.Item(i-1)));
         END;
         
       END;
     END;

    [TryFunction]
     LOCAL PROCEDURE DownloadFileFromFTP@1240060029(FileToDownload@1240060010 : Text);
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Download FilePath" + FileToDownload);
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'RETR';
       FTPWebResponse := FTPWebRequest.GetResponse();
       ResponseStream := FTPWebResponse.GetResponseStream();
       TempBlob.Blob.CREATEOUTSTREAM(OutStream);
       COPYSTREAM(OutStream,ResponseStream);
       FileName := FTPSetup."FTP Local Download FilePath" + FileToDownload; // 'download' + FORMAT(CURRENTDATETIME,0,'<Year4><Month,2><Day,2><Hours24><Minutes,2><Seconds,2>') +'.txt';
       TempBlob.Blob.EXPORT(FileName);
     END;

Please leave your comments, feedback or any suggestions you have for me to improve my blog and also if you have any questions, feel free to post.


Share:

50 comments:

Reeta Devi said...

Great post, I like the info you share about Affordable ftp hosting

rose said...

I wish to indicate because of you only to bail me out of this specific trouble.As a consequence of checking through the net and meeting systems that were not beneficial, I thought my life was finished

Java Training in Bangalore|

Adam lee said...

It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
I’ve bookmarked your site, and I’m adding your RSS feeds to my Google account.
dot net training in bangalore

DAVIS MILLER said...

Your good knowledge and kindness in playing with all the pieces were
very useful. I don’t know what I would have done if I had not
encountered such a step like this.

Selenium Training in Bangalore

Careen joseph said...

Appreciating the persistence you put into your blog and detailed information you provide.
Besant technologies Marathahalli

Inigo joseph said...

Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!
rprogramming training in bangalore

Inigo joseph said...

Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.
rprogramming training in bangalore

mary Brown said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

srihariparu said...

Thanks for sharing this informative blog..
Electrical Project Center in Chennai | Electrical Project Center in Velachery

Joel Wong said...

Hello, I just wanted to leave a comment on how much I love your website looks! Great choice of font and color theme, and that background! By the way, if you ever need help to register a business, let me know, I know the best company secretarial services consultant, register a business immediately!

ASO Services said...

Thanks a lot for sharing this with all of us, I like it and we can communicate. Do you need buy app ratings and reviews. To boost app ranking and double app downloads now.

PLC Training Chennai said...

Good Post..Thanks for sharing



PLC Training in Chennai | PLC Training Institute in Chennai | PLC Training Center in Chennai | PLC SCADA Training in Chennai | PLC SCADA DCS Training in Chennai | Best PLC Training in Chennai | Best PLC Training Institute in Chennai | PLC Training Centre in Chennai | Automation Training in Chennai | Automation Training Institute in Chennai

VLSI Training Chennai said...

I read your blog its very attractive and impressive....

VLSI Training in Chennai | Best VLSI Training in Chennai | VLSI Training Centres in Chennai | VLSI Courses in Chennai | VLSI Training Courses in Chennai | VLSI Training Institute in Chennai | VLSI Training Institutes in Chennai | Best VLSI Training Institute in Chennai

Divya D said...

Informative blog... Thnq for sharing your information with us ...Continue more

Selenium training in Velachery | Selenium training in chennai

Unknown said...

mytectra placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance.

Naveen Yadav said...

How to delete a from FTP server.

Mirnalini Sathya said...

I would like to be a regular contributor to your blog. Your information is really useful for a beginner.
Selenium Training in Chennai
selenium Classes in chennai
iOS Training in Chennai
Best Android Training Institute in Chennai
Best Android Training in chennai

Anjali Siva said...

Thanks for sharing this information admin, it helps me to learn new things. Continue sharing more like this.
Angularjs Training in Chennai
Angularjs Training
ReactJS Training in Chennai
RPA Training in Chennai
AWS course in Chennai
DevOps Certification Chennai

nanadhini chandran said...

I am feeling great to read this.you gave a nice info for us.
please update more.
cloud computing courses near me
Cloud Computing Training in Amjikarai
Cloud Computing Training in Thirumangalam
Cloud computing courses in Bangalore

pavithra dass said...

Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
Good discussion.
CCNA Training in Chennai
DevOps Training in Chennai
DevOps certification Chennai
DevOps certification
CCNA Training
CCNA courses in Chennai

akshaya raja said...

In the beginning, I would like to thank you much about this great post.
I like your writing style and I hope you will keep doing this good working.
Best Spoken English Class in Chennai
Spoken English classes in Chennai
Spoken English Chennai
English Classes in Chennai
Spoken English Course in Chennai
Spoken English Institute in Chennai

vishnu said...

Excellent post. I am really impressed the way you have written the article. I have gained some insight knowledge about the topic

Linux Course in Chennai | Linux Training | Learn Linux | Linux Course in Adyar | Linux Course in Tambaram | Linux Course in Velachery

sharmi chithra said...

It is very excellent blog and useful article thank you for sharing with us, keep posting.

Spoken English Course in Chennai
English Speaking Course in Chennai
Spoken English Training center in Chennai
Spoken English Classes in Anna Nagar
Spoken English Training in Velachery
Spoken English Training in Tambaram
Spoken English Training in Adyar

nanadhini chandran said...

I am feeling great to read this.you gave a nice info for us.
please update more.
software testing courses in bangalore with placement
Software Testing courses in Anna Nagar
Software Testing Certification Training in T nagar
Software Testing Training in Sholinganallur

mercyroy said...

Brilliant ideas that you have share with us.It is really help me lot and i hope it will help others also.update more different ideas with us.
german classes in bangalore
german language course in bangalore
German Training in Thirumangalam
German Training in Nungambakkam

mercyroy said...

Your blog is so inspiring for the young generations.thanks for sharing your information with us and please update more new ideas.
AWS Certification Training
AWS Training Institutes in Bangalore
AWS Training in anna nagar
AWS Training in T nagar

LindaJasmine said...

Awesome Post. Your writing is very unique. The choice of words stood as an exemplary piece of work. Thanks of sharing.
Html5 Training in Chennai
Html5 Courses in Chennai
Html5 Training
Html5 Course
Html5 Training Course
Html5 Training for Beginners
Best Html5 Course
Best Html5 Training

Anbarasan14 said...

This was helpful and thanks for sharing this useful information. Kindly continue the work.

Best English Speaking Course in Mumbai
English Classes in Mumbai
Best Spoken English Classes in Mumbai
English Speaking Training Center in Mumbai
Spoken English Coaching Institute in Mumbai
Best English Classes in Mumbai
Best English Speaking Training near me

Praylin S said...

This seems to be a great article. I would have stuck in the middle if I encounter such problem. Now I've got an idea and I can use it when required. Thanks for sharing this informative post.
Javascript Training in Chennai | Javascript Training Center in Chennai | Javascript Training Classes | Javascript Training

venu bharath said...

Awesome Post. The choice of words makes this post very unique. Great content. Thanks for sharing.
Node JS Training in Chennai
Node JS Course in Chennai
Node JS Advanced Training
Node JS Training Institute in chennai
Node JS Training Institutes in chennai
Node JS Course

ajay prakash said...

Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information
Aviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
best aviation academy in chennai
air hostess course fees structure in chennai
Airport Management Training in Chennai
Ground staff training in Chennai

Vicky Ram said...

Really very nice blog information for this one and more technical skills are improve,i like that kind of post.

Article submission sites
Education

kavithasathish said...

Good job and great informative blog.
Japanese Classes in Chennai
Japanese Course in Chennai
Best Spoken English Classes in Chennai
French Language Classes in Chennai
pearson vue exam centers in chennai
German Classes in Chennai
Japanese Classes in Tnagar
Japanese Classes in OMR

luckys said...

best rice cookers

Aparna said...

Nice post...! I really happy to visit your post and do a post like this more with more info. well done, keep it up the great work!!!
Spark Training in Chennai
Spark Training Academy Chennai
Linux Training in Chennai
Oracle Training in Chennai
Social Media Marketing Courses in Chennai
Power BI Training in Chennai
Tableau Training in Chennai
Excel Training in Chennai
Oracle DBA Training in Chennai
Spark Training in Valachery

sheela rajesh said...

Awesome informations that you have shared for us.I eagerly waiting for more updates in future.
Hadoop Training in Chennai
Big data training in chennai
Hadoop Training in Anna Nagar
JAVA Training in Chennai
Python Training in Chennai
Selenium Training in Chennai
Hadoop Training in Tambaram
Big Data Training in Tambaram
Hadoop Training in Adayar
Big Data Training in Adayar

tech zone said...

scrabble word seek

Lucky Patcher said...

vumoo

harish sharma said...

xmovies8

deepak kumar said...

games whatsapp groups

SAP Training in Chennai said...

Very Good Blog. Highly valuable information have been shared. Highly useful blog..Great information has been shared. We expect many more blogs from the author. Special thanks for sharing..
SAP Training in Chennai | AWS Training in Chennai | Android Training in Chennai | Selenium Training in Chennai | Networking Training in Chennai

vijay said...


I believe there are many more pleasurable opportunities ahead for individuals that looked at your site
linux training in chennai | linux course in chennai| best linux training in chennai |linux certification in chennai

Anbarasan14 said...

I have been reading all your blogs regularly..I admit, this is one of the best blogs I have read till date. Great going.. Waiting for the next...
Spoken English Classes in Chennai
Spoken English in Chennai
Top 10 Spoken English Classes in Chennai
Best IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Classes in Mumbai
English Speaking Classes in Mumbai
Best IELTS Coaching in Mumbai
IELTS Coaching in Mumbai
Spoken English Class in Anna Nagar

rutmanaish said...

I really liked and I got some innovative ideas for improving my thoughts from well defined content.
IELTS Coaching in Chennai
IELTS Classes in Chennai
french courses in chennai
pearson vue
Blockchain Training in Chennai
Ionic Training in Chennai 
IELTS Coaching in Porur
IELTS Coaching in Adyar

The India said...

Rice Bags Manufacturers
Pouch Manufacturers
wall putty bag manufacturers
fertilizer bag manufacturers
seed bag manufacturers
gusseted bag manufacturers
bopp laminated bags manufacturer
Lyrics with music

The India said...

we have provide the best ppc service.
ppc company in gurgaon
website designing company in Gurgaon
PPC company in Noida
seo company in gurgaon
PPC company in Mumbai
PPC company in Chandigarh
Digital Marketing Company

The India said...

we have provide the best fridge repair service.
fridge repair in faridabad
Videocon Fridge Repair in Faridabad
Whirlpool Fridge Repair in Faridabad
Hitachi Fridge Repair In Faridabad
Washing Machine Repair in Noida
godrej washing machine repair in noida
whirlpool Washing Machine Repair in Noida
IFB washing Machine Repair in Noida
LG Washing Machine Repair in Noida

The India said...

iso certification in noida
iso certification in delhi
ce certification in delhi
iso 14001 certification in delhi
iso 22000 certification in delhi
iso consultants in noida

The India said...

iso registration in delhi
iso 22000 certification cost
ISO 9001 Certification in Noida
website designing services
SEO Service Consultant

Abhishek rajpoot said...

WhatsApp Status Video Download :WhatsApp introduced the status feature in 2015, in which we can share images, videos, and gifs as our story for 24 hours. Before this feature, WhatsApp had only text status option in which we can write our bio, but the new status feature is different. The story or status disappears after 24 hours and can’t be archived as still in WhatsApp.

Boy attitude status video download for whatsApp
Boy attitude status video download
Boy attitude status video download

Most romantic status video download for whatsApp
Sad video status download
Most Romantic status video download

video status download for whatsApp


we have latest & best collection of video status download for whatsapp