none
Check the name of a file on FTP RRS feed

  • Question

  • Hi,

    When my app start i want to check the name of a file on FTP.

    Like when the file changes name,  the app downloads files.

    But how to do ?

    Thanks

    Tuesday, May 1, 2018 9:34 PM

All replies

  • Here is an example that reads a version number from file on the server and gives options to do something. Change the serverpath to your file location.

            Dim ServerPath As String = "http://www.your url.txt"
    
            Using wc As New System.Net.WebClient
                Dim NewVersion As New Version(wc.DownloadString(ServerPath))
                Dim OldVersion As Version = My.Application.Info.Version
    
                If NewVersion > OldVersion Then
                    Dim r As DialogResult = MessageBox.Show(String.Format("You have version {0} installed. Would you like to update to the latest version {1}?", OldVersion, NewVersion), "Update SecretStartup?", MessageBoxButtons.YesNo)
                    If r = DialogResult.Yes Then
                        'run a setup program for updates
                        'Process.Start()
                    End If
                End If
            End Using
    

    Tuesday, May 1, 2018 9:58 PM
  • How could an app detect file name changes on an FTP server?

    Perhaps you mean if a file no longer exists that existed before download a different file which doesn't make sense.

    Or perhaps you mean if a file is now at the FTP server that wasn't there before download the new file.

    Well it's really up to you which file(s) to download from an FTP server. I suppose you could save a file that has all the filenames on an FTP server and download new ones that are not in the file.

    Or maybe you mean if a file changes but has the same name download the newer file.

    You really need to be a little more specific in what you want to do.


    La vida loca

    Tuesday, May 1, 2018 10:43 PM
  • How could an app detect file name changes on an FTP server?

    Perhaps you mean if a file no longer exists that existed before download a different file which doesn't make sense.

    Or perhaps you mean if a file is now at the FTP server that wasn't there before download the new file.

    Well it's really up to you which file(s) to download from an FTP server. I suppose you could save a file that has all the filenames on an FTP server and download new ones that are not in the file.

    Or maybe you mean if a file changes but has the same name download the newer file.

    You really need to be a little more specific in what you want to do.


    La vida loca

    Soory i'm french but thanks for you patience :-)

    So this proposition is even better than what I think if you know how to do it?


    Wednesday, May 2, 2018 5:06 PM
  • There are several possibilities in what I posted. Some not viable.

    What is it you need to do exactly as you make no mention of what the actual requirement is?


    La vida loca

    Wednesday, May 2, 2018 7:26 PM
  • There are several possibilities in what I posted. Some not viable.

    What is it you need to do exactly as you make no mention of what the actual requirement is?


    La vida loca

    Sorry I thought I put it "Or maybe you mean if a file changes but has the same name download the newer file."

    I have a software, which must check at each startup if an update of another software is up to date. But I do not really know how

    Wednesday, May 2, 2018 10:01 PM
  • Sorry I thought I put it "Or maybe you mean if a file changes but has the same name download the newer file."

    I have a software, which must check at each startup if an update of another software is up to date. But I do not really know how

    That is not a file name changed issue. That is comparing two files hash to each other to verify if they have the same hash value. In that way you can know if the two files are different. Because it's always possible the two files could have the exact same size and then you would not know if they are different.

    Even if the two files had different dates that would not mean they are not the same files.

    The simplest method I suppose would be download the file with the same name to temp storage, get its hash and the hash of the same file already on the system and compare the hashes. If the file on the FTP server then has a different hash you can assume it is changed regardless of any other information about it as long as it and the file on the system have the same name.

    I don't believe you could use the file properties on the FTP server as a guide for determining the possibility of a change but I'm not 100% sure.


    La vida loca

    Wednesday, May 2, 2018 10:08 PM
  • Sorry I thought I put it "Or maybe you mean if a file changes but has the same name download the newer file."

    I have a software, which must check at each startup if an update of another software is up to date. But I do not really know how

    That is not a file name changed issue. That is comparing two files hash to each other to verify if they have the same hash value. In that way you can know if the two files are different. Because it's always possible the two files could have the exact same size and then you would not know if they are different.

    Even if the two files had different dates that would not mean they are not the same files.

    The simplest method I suppose would be download the file with the same name to temp storage, get its hash and the hash of the same file already on the system and compare the hashes. If the file on the FTP server then has a different hash you can assume it is changed regardless of any other information about it as long as it and the file on the system have the same name.

    I don't believe you could use the file properties on the FTP server as a guide for determining the possibility of a change but I'm not 100% sure.


    La vida loca

    Ok I did not think about hash, but it's a good idea. But can you show me a code example with what you said. I have no idea how to do that. Do you have a file hosting alternative than FTP?

    Thursday, May 3, 2018 11:08 AM
  • Ok I did not think about hash, but it's a good idea. But can you show me a code example with what you said. I have no idea how to do that. Do you have a file hosting alternative than FTP?

    Well I'm not a webserver guy so somebody else would have to provide an alternative to a hosting server.

    Getting the hash of a file is fairly easy. I'll update this thread with that in a few minutes.

    Update: See the following links. The below code is an example for using MD5.

    Hash and Signature Algorithms

    HashAlgorithm.ComputeHash Method (Byte())

    HashAlgorithm.ComputeHash Method (Stream)

    MD5.ComputeHash Method

    Option Strict On
    
    Imports System.Security.Cryptography
    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)))
            With Label1
                .BorderStyle = BorderStyle.FixedSingle
                .BackColor = Color.White
                .Text = "Waiting"
                .AutoSize = False
                .Size = New Size(250, 250)
                .MinimumSize = New Size(250, 250)
                .MaximumSize = New Size(250, 250)
                .Left = CInt((Me.ClientRectangle.Width / 2) - (Label1.Width / 2))
            End With
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Label1.Text = "Waiting"
            Using OFD As New OpenFileDialog
                OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                OFD.Title = "Get MD5 Hash of file"
                OFD.Multiselect = False
                OFD.Filter = "All Files (*.*)|*.*"
                If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Dim MD5 = System.Security.Cryptography.MD5.Create
                    Dim Hash As Byte()
                    Dim sb As New System.Text.StringBuilder
                    Using st As New IO.FileStream(OFD.FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
                        Hash = MD5.ComputeHash(st)
                    End Using
                    For Each b In Hash
                        sb.Append(b.ToString("X2"))
                    Next
                    Label1.Text = OFD.FileName & " hash = " & vbCrLf & sb.ToString
                End If
            End Using
        End Sub
    
    End Class


    La vida loca

    Thursday, May 3, 2018 3:54 PM
  • Ok I did not think about hash, but it's a good idea. But can you show me a code example with what you said. I have no idea how to do that. Do you have a file hosting alternative than FTP?

    Well I'm not a webserver guy so somebody else would have to provide an alternative to a hosting server.

    Getting the hash of a file is fairly easy. I'll update this thread with that in a few minutes.

    Update: See the following links. The below code is an example for using MD5.

    Hash and Signature Algorithms

    HashAlgorithm.ComputeHash Method (Byte())

    HashAlgorithm.ComputeHash Method (Stream)

    MD5.ComputeHash Method

    Option Strict On
    
    Imports System.Security.Cryptography
    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)))
            With Label1
                .BorderStyle = BorderStyle.FixedSingle
                .BackColor = Color.White
                .Text = "Waiting"
                .AutoSize = False
                .Size = New Size(250, 250)
                .MinimumSize = New Size(250, 250)
                .MaximumSize = New Size(250, 250)
                .Left = CInt((Me.ClientRectangle.Width / 2) - (Label1.Width / 2))
            End With
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Label1.Text = "Waiting"
            Using OFD As New OpenFileDialog
                OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                OFD.Title = "Get MD5 Hash of file"
                OFD.Multiselect = False
                OFD.Filter = "All Files (*.*)|*.*"
                If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Dim MD5 = System.Security.Cryptography.MD5.Create
                    Dim Hash As Byte()
                    Dim sb As New System.Text.StringBuilder
                    Using st As New IO.FileStream(OFD.FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
                        Hash = MD5.ComputeHash(st)
                    End Using
                    For Each b In Hash
                        sb.Append(b.ToString("X2"))
                    Next
                    Label1.Text = OFD.FileName & " hash = " & vbCrLf & sb.ToString
                End If
            End Using
        End Sub
    
    End Class


    La vida loca

    Thank you very much for this precious help. Now you have to know how to use this on a WEB host
    Thursday, May 3, 2018 6:21 PM
  • Thank you very much for this precious help. Now you have to know how to use this on a WEB host

    Hi theopom,

    You project is asp.net? If yes, maybe you need to ask your question at asp.net forum.

    https://forums.asp.net/

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 8, 2018 9:18 AM
    Moderator