如何在vb.net中将所有最新文件从ftp服务器自动下载到pc

问题描述:

大家好,

我需要帮助来创建vb.net Windows服务,每隔1分钟从服务器将最新的新上传文件下载到本地计算机.服务启动后,此代码将下载相同的文件

I need help to create a vb.net windows service to download latest new uploaded files from server after every 1 minute to local computer . This code download same file once the service started

我要服务以检测服务器上载的最新新文件并自动下载.下面是我的代码.请帮助我.

I want to service detect the latest new file uploaded on server and download it automatically.  Below is my code. Kindly help me.

Private Sub FTPDownloadFile()

        Dim ftpuri As String = ""
        Dim downloadpath As String = ""
        Dim ftpusername As String = ""
        Dim ftppassword As String = ""

        'Create a WebClient.
        Dim request As New WebClient()

        ' Confirm the Network credentials based on the user name and password passed in.
        request.Credentials = New NetworkCredential("root", "")

        'Read the file data into a Byte array
        Dim bytes() As Byte = request.DownloadData("http://localhost/salary_system/home.php")



        Try
            '  Create a FileStream to read the file into
            For Each i As String In downloadpath

                Dim DownloadStream As FileStream = IO.File.Create("C:\download\home.php")
                '  Stream this data into the file
                DownloadStream.Write(bytes, 0, bytes.Length)
                '  Close the FileStream
                DownloadStream.Close()
            Next

        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try

        MsgBox("Process Complete")

    End Sub






结束班级







End Class


首先,您应该使用代码块在帖子中插入代码.而且您的代码中没有任何内容与FTP有关.

Well first you should use a code block for inserting code in a post. And nothing in your code concerns FTP.

__________________________________________________

__________________________________________________

无论如何,请查看/阅读以下主题.它们中可能还包含链接,您应该阅读.

Anyhow view/read the below threads. They may also have links in them you should read.

如果上载到FTP服务器的新文件的名称与以前从FTP服务器下载的文件的名称相同,但是上次写入日期不同,或者是较新的版本,则该怎么办?

What if new files uploaded to FTP server have same name as previously downloaded files from FTP server but have different last write date or are newer version or something?

无论如何,我想您必须确定FTP服务器上的任何文件是否存在(无论将它们下载到何处).因此,我想您将不得不使用文件名比较. IO.Path.GetFileName 可以从文件路径的路径/文件名中获取文件名.

Anyhow I suppose you would have to determine if any files on the FTP server already exist to wherever you download them to. Therefore I suppose you would have to use a filename comparison. IO.Path.GetFileName can get the name of a file from the path/filename of a path for a file.

因此,一旦您的FTP正常工作,那么如果您还有其他问题,请提出另一个问题.然后还提供您正在使用的代码以及问题发生的位置或问题所在.在代码下方的第三个链接上 仍然应该与URL中使用的FTP站点一起使用,该站点可以免费使用FTP站点进行下载.

So once you get your FTP working then if you have other issues then ask a different question. And then also provide the code you are using along with where the issue is occuring or what the issue is that you are having. At the third link below the code I wrote should still work with the FTP site used in the URL which is a free to use FTP site for downloading.

How to read the files from ftp server and display at textbox in vb.net

此FTP站点将允许您上载进行测试.每个上载必须具有不同的文件名.上传的文件会在48小时后" ftp://ftp.uconn.edu/48_hour/"删除.

This FTP site will let you upload for testing. Each upload must have a different file name. The uploads are deleted after 48 hours "ftp://ftp.uconn.edu/48_hour/".

我在该网站上使用的以下代码.我相信您也许可以下载该位置中的所有文件,但我不记得了.

The code below I used with that site. I believe you may be able to download any files that are in the location but I don't remember.

Option Strict On

Imports System.Net
Imports System.IO
Imports System.Text

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
    End Sub

    Dim Rand As New Random
    Dim FilePathAndName As String = ""

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Temp() As String = IO.Directory.GetFiles(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Text Files"), "*.Txt", IO.SearchOption.TopDirectoryOnly)
        Dim FileToGet As Integer = Rand.Next(0, Temp.Count)
        FilePathAndName = Temp(FileToGet)
        MessageBox.Show(Temp.Count.ToString & vbCrLf & FileToGet.ToString & vbCrLf & FilePathAndName)
        'ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType) ' For using TLS possibly

        Try
            Dim FTPUpload As FtpWebRequest = DirectCast(WebRequest.Create("ftp://ftp.uconn.edu/48_hour/" & IO.Path.GetFileName(FilePathAndName)), FtpWebRequest)
            FTPUpload.Method = WebRequestMethods.Ftp.UploadFile
            FTPUpload.Credentials = New NetworkCredential("anonymous", "User@")
            FTPUpload.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Anonymous
            'FTPUpload.EnableSsl = True
            Dim FTPResp As FtpWebResponse
            Dim b() As Byte
            Using SR As New IO.StreamReader(FilePathAndName)
                b = Encoding.UTF8.GetBytes(SR.ReadToEnd())
            End Using
            FTPUpload.ContentLength = b.Length
            Using RS As Stream = FTPUpload.GetRequestStream()
                RS.Write(b, 0, b.Length)
                FTPResp = DirectCast(FTPUpload.GetResponse(), FtpWebResponse)
            End Using
            MessageBox.Show(FTPResp.StatusDescription)
            FTPResp.Close()
        Catch ex As System.Net.WebException
            MessageBox.Show(ex.ToString & vbCrLf & ex.StackTrace)
        End Try
    End Sub

End Class