Fred Mastropasqua's Facebook profile

FTP with VB.Net

by Fred Mastro 8. May 2009 03:57

Someone recently mentioned they need to do some FTP stuff in their application, send up files and such to a remote server. Well, low and behold, I’ve already done that task before. So I thought I’d provide them some code to use.

It’s actually pretty easy, you just have to setup some Constents and then you could basically copy and paste my methods.

In this example I’m uploading a specific filename. Wouldn’t take much to modify to upload multiple’s.

Private _ServerFriendlyName As String = ConfigurationManager.AppSettings("FTPServerFriendlyName") '"Production Box Alpha"
Private _ServerAddressURI As String = "ftp://" & ConfigurationManager.AppSettings("FTPServerIP") ' 172.1.1.20"
Private _ServerPath As String = "" '"/Logs"
Private _FTPUserName As String = ConfigurationManager.AppSettings("FTPUserName") '"ftpsusername"
Private _FTPPassword As String = ConfigurationManager.AppSettings("FTPPassword") ' "ftppass"
Private _FileToUploadFileName As String = "FileToUpload.txt"

Let’s go over the variables I have here.

_ServerFriendlyName – Just a var I use to show what server you are going too
_ServerAddressURI – this is the FTP address, feel free to use IP for FDQN
_ServerPath – this is where on the server I will dump my files into, in this case the user/pass already go into the /logs folder so this var is blank
_FTPUsername – obvious
_FTPPassword – obvious
_FTPToUploadFileName – this is the file I am planning to upload and download

On my page_load I am calling a DownloadFile function to get the file.

DownloadFile("/Logs/" & _FileToUploadFileName)

And here is the first copy function, the Download Method

Private Sub DownloadFile(ByVal FTPPath As String)
    Try
        Dim ListRequest As FtpWebRequest = WebRequest.Create(_ServerAddressURI & FTPPath)
        Dim Crendentials As New NetworkCredential(_FTPUserName, _FTPPassword)
        ListRequest.Credentials = Crendentials
        ListRequest.Method = WebRequestMethods.Ftp.DownloadFile
 
        Dim FTPResp As FtpWebResponse = ListRequest.GetResponse
        Dim FTPRespStream As Stream = FTPResp.GetResponseStream
        Dim Reader As New StreamReader(FTPRespStream, System.Text.Encoding.UTF8)
 
        Dim FileName = FTPPath.Substring(InStrRev(FTPPath, "/"))
        Dim FileLocation As String = HttpContext.Current.Request.MapPath("~/Logs/" & FileName)
        Dim DownloadedFile As New FileStream(FileLocation, FileMode.Create)
        Dim oWriteFile As New StreamWriter(DownloadedFile)
        oWriteFile.Write(Reader.ReadToEnd)
 
        FTPResp.Close()
        oWriteFile.Close()
        DownloadedFile.Close()
    Catch ex As Exception
        'Place your Error Handling Here
    End Try
End Sub

Afterwards, the user can modify the file as needed and then I upload it back when they are done. Notice I have on line 5, the MapPath(“~/Logs"/”….  that is the local path on the web server.  This should probably also be set at the top of my app and yours.  You send in the path you want to upload too

UploadFile("/Logs/" & _FTPToUploadFileName)
Private Sub UploadFile(ByVal FTPPath As String)
    Try
        Dim FileName As String = _ServerAddressURI & FTPPath
        Dim FileLocation As String = ""
        FileLocation = HttpContext.Current.Request.MapPath("~/Logs/" & _FTPToUploadFileName)
 
        Dim Client As New WebClient
        Client.Credentials = New NetworkCredential(_FTPUserName, _FTPPassword)
        Client.UploadFile(FileName, FileLocation)
    Catch ex As Exception
        'Put your Error Handling Here
    End Try
End Sub

Yes there are tweaks and that can be made, but you have enough to basically copy/paste and get a huge jump start.  You could also stream the file in from memory.

What was that? You want this code in c#? Well here you go, convert it :)

Tags: , , , ,

VB.NET | Programming | Code Snippets

Comments

5/17/2009 11:40:43 PM #

trackback

FTP with VB.Net

You are voted (great) - Trackback from Web Development Community

Web Development Community |

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

A Certified MCSE (NT4 & 2k), MCDBA (2k), A+, CCA, with over 10 years of experience with Windows Networking and Development. Developing mainly in ASP.NET, VB.NET and T-SQL. Also develops in Objective-C (iPhone), XAML (SilverLight & WPF), C#, "Classic" ASP 3.0, ADSI,  VBscript, WScript.

Non-technical hobbies include other areas such as Movie watching (action, epic, comedy, some romantic comedy, well everything), Reading (Science Fiction, Fantasy, Detective and Programming categories), Film Editing, Directing with Special effects (using Adobe Premiere and Adobe After Effects), Dungeons & Dragons (D&D 4th Edition), Auto-Cross Racing & Cars (BMW M3, MazdaSpeed's), Motorcycles (Honda CBR 600), TV Shows (Flight of the Conchords, Lie To Me, DollhouseBattleStar Galatica, Smallville, Alias), Music (Akon, Billy Joel, Micheal Bublé, Bid Daddy Weave, T-Pain, Barlow Girl, Notorious B.I.G and more, love all types of music), and Religion (Christianity, debating and prophecies).


Web Tools - QuickLinks

Web tools I use more then others. Some of these are on my Link Collections page, but this made it easier for me to go to my site and click a tool.

  1. Telerik Code Converter (C# to VB/VB to C#)
  2. Lorem Ipsum - Dummy Text for Prototype Apps
  3. Web Color Values
  4. Open Source Icons

 

Highlights

  • Some websites I've worked on. This is a small collection of sites I've developed or added to awhile back.

  • Revenge Movie Trailer. Trailer I made with Adobe Premiere and After Effects. Jason Christman is the main star and I'm the director behind the camera.

  • Essential Software For your Mac. - I'm a Microsoft geek, but I've switched over to Mac. There was a lot of stuff I needed to get installed that I missed on my Windows machine. Also I had no idea how to do it :p Here's some help.

  • Speed Football. I wanted to make a special effect like the Smallville or Superman running fast. All the other ones I've seen, the person in the frame was the only moving object while everything else was blurred. I wanted to create the effect but interact with another normal moving object.
  •    
  • Code Snippets and Quickies. Sometims I find something or develop something that I think is useful and it can be copied and pasted anywhere for someone to use. Here's a collection of things I've posted on.
  • Books I've Read or Reading