Solved

Get File from FTP Server using VB.Net

Posted on 2015-01-20
5
551 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 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

8 Experts available now in Live!

Get 1:1 Help Now