Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a few lines of code here from a sample project.

Function md5_hash(ByVal file_name As String)
        Return hash_generator("md5", file_name)
    End Function

    Function hash_generator(ByVal hash_type As String, ByVal file_name As String)
        Dim hash
        hash = MD5.Create

        Dim hashValue() As Byte

        Dim filestream As FileStream = File.OpenRead(file_name)
        filestream.Position = 0
        hashValue = hash.ComputeHash(filestream)
        Dim hash_hex = PrintByteArray(hashValue)
        filestream.Close()

        Return hash_hex
    End Function


    Private Sub BunifuThinButton21_Click(sender As Object, e As EventArgs) Handles BunifuThinButton21.Click
    Dim path As String = OpenFileDialog1.FileName
    Dim sample As String
                sample = md5_hash(path)
End Sub

As you can see in this code, an OpenFileDialog was used to declare the path of the file that is being checked to see if it has an MD5 hash matching a database of MD5 hashes.

Now, in my program, I have a FileSystemWatcher which is supposed to monitor all of the files being opened by the user, but I can't seem to be able to create that function, because I'm not sure if there's a way to declare the file for 'path' when it's being opened.

How do you do this?

Leave any questions below, and I'll do my best to talk to you about it. I'm only a beginner at VB.net, so I've been trying my best to find a solution to this.

What I have tried:

Asking questions on other sites.
Posted
Updated 4-Jun-18 1:14am
v2

1 solution

Look at the documentation: FileSystemWatcher Class (System.IO)[^] - it includes example code which uses the Changed event and fetches the path of the altered file in the handler!
 
Share this answer
 
Comments
Member 13242613 4-Jun-18 7:22am    
Ok, thanks, I've looked at it. I hate asking these things, but can you put it into my code? Here is my FileSystemWatcher code:

Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

Dim path As String

FileSystemWatcher1.EnableRaisingEvents = True
Dim sample As String
sample = md5_hash(path)

Using f As System.IO.FileStream = System.IO.File.OpenRead("viruslist.txt")
Using s As System.IO.StreamReader = New System.IO.StreamReader(f)
While Not s.EndOfStream
Dim line As String = s.ReadLine

If (line = sample) Then
Detect.Show()
Else

End If
End While
End Using
End Using




End Sub


These examples are just all new to me with their code. @OriginalGriff
OriginalGriff 4-Jun-18 7:31am    
Oh come on!
You can't cope with

Dim path As String = e.FullPath

without help?
I think you need to re-read your course notes...
Member 13242613 4-Jun-18 7:37am    
Sorry. Stupid question.


However, I've added in some things from the example, and its saying that access to "C:\" is denied.
OriginalGriff 4-Jun-18 7:47am    
Well that doesn't surprise me - a lot of the files in the root are restricted to admin access.

Use the debugger to find out which files are involved, and see if you can check their permissions.
Member 13242613 4-Jun-18 7:53am    
Ok, will do.

Quick question, is there something like a 'Directory.OpenRead' tab? Or 'Directory.ReadFiles'? I'm thinking that on the Dim filestream As FileStream = File.OpenRead(file_name), I can use something like that to look in the directory.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900