Here's an update. I'm now using serilization to save and load my files, which save from, and load to the arrays acceptarray or unacceptarray. I then check to see if my processes() array contains any of the entries of the acceptarray or unacceptarray. If it finds a match under the acceptarray, nothing happens, if it finds a match under the unacceptarray, the process is killed and the user is notified in a messagebox. It it is not found on either list, the user is notified and must choose which list it belongs in.
The problem I'm having is that It isn't finding the processes that should be in acceptarray, or the ones in unacceptarray. I'm going to post the majority of my code below, I hope someone can point out my error(s). Sorry, I know It's a lot of code to go through.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim processes() As System.Diagnostics.Process
Dim process As New System.Diagnostics.Process
Dim acceptcheck As New Acceptable
Dim unacceptcheck As New Unacceptable
If IO.File.Exists(Application.StartupPath & "\accept.cfg") = False Then
Dim myFileStream As New FileStream(Application.StartupPath & "\accept.cfg", FileMode.OpenOrCreate) 'Open the file stream...
Dim myBinaryFormatter As New BinaryFormatter
'adding some windows process to the accept list, so it doesn't try to deserialize an empty stream later
acceptcheck.Strprocess = "svchost"
acceptarray.Add(acceptcheck)
acceptcheck.Strprocess = ""
acceptcheck.Strprocess = "explorer"
acceptarray.Add(acceptcheck)
acceptcheck.Strprocess = ""
acceptcheck.Strprocess = "taskmgr"
acceptarray.Add(acceptcheck)
acceptcheck = Nothing
myBinaryFormatter.Serialize(myFileStream, acceptarray) 'Using our formatter, write the alAllUsers to disk.
myFileStream.Close()
myBinaryFormatter = Nothing
End If
If IO.File.Exists(Application.StartupPath & "\unaccept.cfg") = False Then
Dim myfilestream As New FileStream(Application.StartupPath & "\unaccept.cfg", FileMode.OpenOrCreate)
Dim mybinaryformatter As New BinaryFormatter
'I chose winamp just for testing purposes, and so it didn't deserialize an empty stream
unacceptcheck.Strprocess = "winamp"
unacceptarray.Add(unacceptcheck)
unacceptcheck = Nothing
mybinaryformatter.Serialize(myfilestream, unacceptarray)
myFileStream.Close()
myBinaryFormatter = Nothing
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim processes() As System.Diagnostics.Process
Dim process As New System.Diagnostics.Process
Dim myFileStream As New FileStream(Application.StartupPath & "\accept.cfg", FileMode.Open)
Dim myfilestream2 As New FileStream(Application.StartupPath & "\unaccept.cfg", FileMode.Open)
Dim myBinaryFormatter As New BinaryFormatter
Dim mybinaryformatter2 As New BinaryFormatter
acceptarray = DirectCast(myBinaryFormatter.Deserialize(myFileStream), ArrayList)
myFileStream.Close()
myBinaryFormatter = Nothing
unacceptarray = DirectCast(mybinaryformatter2.Deserialize(myfilestream2), ArrayList)
myfilestream2.Close()
mybinaryformatter2 = Nothing
processes = process.GetProcesses("MyComputer")
For Each process In processes
If acceptarray.Contains(process.ProcessName) Then
ElseIf unacceptarray.Contains(process.ProcessName) Then
process.Kill()
MessageBox.Show("An unathorized process has been terminated. If this is a legitimate process, please transfer it from the unacceptable list to the acceptable list." & vbNewLine & "Process Name: " & process.ToString, "Unathorized Process", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Else
MessageBox.Show("A process is trying to run for the first time. Add it to the acceptable processes list? Choosing no will automatically add the process to the unacceptable process list" & vbNewLine & process.ToString, "New Process", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
End If