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|

Unknown 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

Unknown said...

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

saranya 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

saranya 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

srihariparu said...

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

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

Unknown said...

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

Selenium training in Velachery | Selenium training in chennai

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

Unknown 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 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

Anonymous 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

Unknown 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

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

luckys said...

best rice cookers

tech zone said...

scrabble word seek

harish sharma said...

xmovies8

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

Prwatech said...

I learned World's Trending Technology from certified experts for free of cost. I Got a job in decent Top

MNC Company with handsome 14 LPA salary, I have learned the World's Trending Technology from Data science training in btm

layout
experts who know advanced concepts which can help to solve any type of Real-time issues in

the field of Python. Really worth trying Freelance seo expert in

Bangalore

varshaDeepak said...

Best Oracle Training in Chennai

akalya said...

nice information....
javascript max int
whatsapp unblock myself software
lady to obtain 10kgs more for rs.100, find the original price per kg?
about bangalore traffic
how to hack whatsapp ethical hacking
the lcm of three different numbers is 1024. which one of the following can never be there hcf?
how to hack tp link wifi
whatsapp unblock hack
sample resume for call center agent for first timers
a merchant sold an article at 10 loss

Garrick Co Ida said...

The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. Machine Learning Final Year Projects In case you will succeed, you have to begin building machine learning projects in the near future.

Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

shri said...

Awesome...
internship report on python
free internship in chennai for ece students
free internship for bca
internship for computer science engineering students in india
internships in hyderabad for cse students 2018
electrical companies in hyderabad for internship
internships in chennai for cse students 2019
internships for ece students
inplant training in tcs chennai
internship at chennai

subha said...

These blog concepts are new and unique it is easy to understand compared to other blogs. Keep updating this blogs.
DOT NET Training in Bangalore
DOT NET Training in Chennai
Dot NET Training in Marathahalli
DOT NET Training Institute in Marathahalli
DOT NET Course in Bangalore
AWS Training in Bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
PHP Training in Bangalore
Spoken English Classes in Bangalore

shreekavi said...


This blog is really awesome. I learned lots of informations in your blog. Keep posting like this.
Selenium Training in Chennai
Selenium Training in Bangalore
Selenium Training in Coimbatore
Best Selenium Training in Bangalore
Selenium Training Institute in Bangalore
Selenium Classes in Bangalore
selenium training in marathahalli
Selenium training in Btm
German classes in bangalore

wilsonkevin said...

My engagement ring is platinum and I couldn't be happier. It is so shiny however the downside is that it's pretty expensive and costs more to size than gold. There are scratches although not very noticeable seaport hack

Mursalin Ahmed said...

They seem to be easy but infact requires a lot of time and hardwork . Great work world of tanks blitz hack

Technogeekscs said...

I am really happy with your blog because your article is very unique and powerful for new.
Data Science
Selenium
ETL Testing
AWS
Python Online Classes

ve may bay tet said...

Aivivu - chuyên vé máy bay, tham khảo

vé máy bay đi Mỹ bao nhiêu tiền

vé bay từ mỹ về việt nam

giá vé máy bay từ Toronto đến việt nam

Các chuyến bay từ Incheon về Hà Nội hôm nay

EMMANUEL said...

thank you for blogging.Keep up the good work

Python Training in chennai | Python Classes in Chennai

Aishwariya said...

Awesome post Python Training in Chennai

Anonymous said...

Very informative post.

vivikhapnoi said...

An awesome blog thanks a lot for giving me this great opportunity to write on this.
vietnam airline đi mỹ

Lịch bay từ Seoul đến Hà Nội

Cách săn vé máy bay giá rẻ tu Nhat Ban ve Viet Nam

vé máy bay từ singapore về sài gòn

Ve may bay Vietnam Airline tu Dai Loan ve Viet Nam

chuyến bay từ canada về việt nam

Dreamsoft Consultants said...

This blog is very useful it include very knowledgeable information. Thankyou for sharing this blog with us. If anyone want to experience certificate in bangalore can call at 9599119376 or can visit https://experiencecertificates.com/experience-certificate-provider-in-bangalore.html

videosaver said...
This comment has been removed by the author.
Upgrade VLSI Technologies said...

Great job for publishing such a nice article. Your article isn’t only useful but it is additionally really informative. Thank you because you have been willing to share information with us. Read more info about Job Oriented Vlsi Courses

Dreamsoft Consultants said...

This blog is very useful for me it gives me the very knowledgeable information to me. Dreamsoft is the 20years old consultancy providing the experience certificate in many status of the India. the interested may contact at the 9599119376 or can visit our website for the
Career Boosting Genuine Experience Certificate In Mumbai
https://experiencecertificates.com/experience-certificate-provider-in-mumbai.html
Career Boosting Genuine Experience Certificate In Gurugram
https://experiencecertificates.com/experience-certificate-provider-in-Gurgaon.html
Career Bosting Genuine Experience Certificate In Delhi
https://experiencecertificates.com/experience-certificate-provider-in-delhi.html
Career Boosting Experience Certificate In Noida
https://experiencecertificates.com/experience-certificate-provider-in-Noida.html
Career Boosting Genuine Experience Certificate In Bangalore
https://experiencecertificates.com/experience-certificate-provider-in-bangalore.html
Career Boosting Genuine Experience Certificate Hyderabad
https://experiencecertificates.com/experience-certificate-provider-in-Hyderabad.html

EZaccounting said...

Nice article, good way of content delivery.
Ubs accounting
Myob Singapore
Skill future accounting software
Best Accounting software Singapore

Morgan said...

Laser printers print far more copies than an inkjet due to the use of a toner. Yes, a toner can certainly crank out a great many more copies than what the ink cartridges from an inkjet. Over time, a toner can prove to save a significant amount of money over time. renting impresoras barcelona

Sris Law said...

This article shares really good content. Very nice to read the article. Keep updating.
Immigration Lawyer Near Me Virginia
Personal Injury Lawyers Near Me Virginia

leylandpartsbpauto said...

If you're in need of quality Suzuki Wagon-R Parts, you can't go wrong with our selection here at Suzuki Alto Spare Parts. We carry a wide range of parts for your car, so you can get back on the road in no time. Plus, our parts are all backed by a 100% satisfaction guarantee, so you can shop with confidence.

GwayERP software said...

Wonderful work! keep sharing articles like this very insightful information and thank you for your great work towards this blog.
software companies in chennai