Solved

Get File from FTP Server using VB.Net

Posted on 2015-01-20
5
542 Views
Last Modified: 2015-01-26
I'm using the following in MS Excel VBA to get a file from a FTP server:

    Dim lReturn As Long
    Dim strFullURL As String
    Dim strLocation As String
   
    strFullURL = "ftp:\\myUserName:myPassword@192.168.100.114\" & strFileName
    strLocation = "c:\DB\" & strFileName
   
    lReturn = URLDownloadToFile(0, strFullURL, strLocation, 0, 0)
   
    If lReturn <> 0 Then Call MsgBox("failed")

This works fine in MS Excel VBA, but when I try to use the same in VB.Net (Visual Basic 2010) I get the following error:

    AccessViolationException was unhandled
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Need to get file(s) from the server using VB.Net. Can anyone provide a solution?
0
Comment
Question by:Tim313
  • 3
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40560755
Try using the WebClient Class which is located in the  System.Net.WebClient namespace.  Once you create a WebClient object you will need to set up the Credentials property with the username and password. then you can use the DownloadFile(String, String) to download the file you need.

At the bottom of the page for the DownloadFile method they show an example of what needs to be done.
0
 

Author Comment

by:Tim313
ID: 40566933
Hi Fernando, sorry for the late response...

I'm already using an FtpWebRequest in my vb.net app. It uses a buffer and reads/writes 2047 bytes at a time taking approx. 6 minutes to download a 20 MB text file.

The code shown in my first post is used in a MS Excel VBA, and file download is almost immediate.

The WebClient Class you suggest appears to use a "reader", similar to the FtpWebRequest. Do you know if the download is in 2047 byte increments or does it download the complete file?
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40567047
Hi Tim;

To your question, "Do you know if the download is in 2047 byte increments or does it download the complete file?", no I do not.

If you want to use the old URLDownloadToFile method in place of the .Net version you can use Win32 system method by using DllImport of urlmon.dll as shown in the pinvoke web site which will work as that you used in VBA.
0
 

Author Closing Comment

by:Tim313
ID: 40570787
Thanks!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40571307
Not a problem Tim, glad to help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now