Solved

Get File from FTP Server using VB.Net

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 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