Solved

Get File from FTP Server using VB.Net

Posted on 2015-01-20
5
589 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 63

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 63

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 63

Expert Comment

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

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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