Text file as database??? Maybe! [2005]
Ok here's the situation. Want to control if certain websites are accessed. I can put in a name such as "yahoo" or "vbcity" or whatever in the code and it works. But I want to be able to add name to the list without rewriting the program everytime I want to add a site. Is this possible with a simple text file?
The code below seems to retrieve the info that I need, but it puts 2 little squares between each name in the text file, I think then it looks for the site name with those 2 little suares added. If I just type one name in the text file it works, but when I type more than one, it doesn't. Thanks in advance.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each proc As Process In Process.GetProcessesByName("iexplore") 'calls internet explorer process
Dim intex As String = proc.MainWindowTitle.ToLower 'changes MainWindowTitle text to lower case
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText("C:\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\textfile1.txt") 'reads text file
If intex.Contains(fileContents) Then
MsgBox("Found Window!")
Else : MsgBox("Could not find window")
End If
Next
End Sub
Thank You,
Michael Ifland
Visual Basic.NET 2005
Re: Text file as database??? Maybe! [2005]
Well first of all you should have all the strings you're comparing do so in lower case, because you may not know the exact casing of each website title.
Second, you're loading a COMPLETE text file, and then checking the process caption against the WHOLE thing, which is a no-no. It won't work, because if you have two sites(i.e. "Yahoo!" and "Google"), it's going to check the process's caption for "Yahoo! Google", and not each individually.
What you could do, is have the text file containing the website titles SEPARATE each title you wish to look for by a line break(VbCrLF) and then scan through the text file line by line and check for each title individually. For example(I'm just modifying your code in the reply window as I don't have VS open at the moment):
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each proc As Process In Process.GetProcessesByName("iexplore") 'calls internet explorer process
Dim intex As String = proc.MainWindowTitle.ToLower 'changes MainWindowTitle text to lower case
Dim fileContents() As String
fileContents = Split(My.Computer.FileSystem.ReadAllText("C:\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\textfile1.txt"), VbCrLf) 'reads text file
For i As Integer = 0 To UBound(fileContents)
If intex.ToLower.Contains(fileContents(i).ToLower) Then
MsgBox("Found Window!") : Exit Sub
Else : MsgBox("Could not find window")
End If
Next
Next
End Sub
Re: Text file as database??? Maybe! [2005]
BrendanDavis,
Thank you so much for your help, that works great. I was also talking with some other folks and they had me doing a "streamread". the only problem was this program was running on a timer every second. It wouldn't let me make any changes to the textfile because it was being used by another process(my program). This configuration allows me to add or take away from the textfile database. Thank you so much.