Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get File from FTP Server using VB.Net

Posted on 2015-01-20
5
Medium Priority
?
605 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 64

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

730 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