What I'm attempting to do (with some success) is scan a directory and it's directories (no other directories below the child), and see if that file and path exist in a database by using
.ExecuteScalar(). If 0 is returned then the file's record in the db has been deleted, and if it's 1 then it still exists.
While scanning the files and folders, I add the file info (file.Name, folder.name and file.Length) into a new row in the DataGridView, and add a check mark to the first column and color the row red if .ExecuteScalar() returns a 0. If 1 is returned then I keep the first column unchecked and color the row green.
When the user clicks on the Delete button, I want to be able to go through all of the rows, and if it's checked then delete that file and folder.
So far the scanning of the files and folders seem to be working, however with only 692Kb of test data (this is suppose to go through ~2 Terrabytes of data) in 39 files in 9 folders this already seems to be a bit sluggish, and takes about 3-4 seconds to load (and the vertical scrollbar loads up weird, is there a way to place a "disabled" greyed out scroll bar?).
My main question is what would be the best way to handle the scanning of files, populating the DatagridView and deleting the appropriate files afterwards? Should I store all of the info into an Array then load the Datagridview, and if the user checks or unchecks a row, then change the value in the array accordingly?
Below is the code that I'm using to scan the files/folders and check the db.
Any help would be greatly appreciated.
Thanks,
Jay
For
Each folder In rootDir.GetDirectories
For Each file In folder.GetFiles
Try
Dim cmd As New OleDbCommand
cmd.CommandTimeout = 60
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sqlQuery = "SELECT Count(Files.ID) FROM Files, Folders " _
+ "WHERE Folders.Folder = '" + folder.Name.ToString + "' " _
+ "AND Files.Folder = Folders.ID " _
+ "AND Files.File = '" + file.Name + "';"
cmd.CommandText = sqlQuery
conn.Open()
If conn.State = ConnectionState.Open Then
recordCount = cmd.ExecuteScalar()
End If
Catch exp As Exception
MessageBox.Show(exp.Message)
Finally
conn.Close()
End Try
If recordCount > 0 Then
dgFiles.Rows.Add(False, file.Name.ToString, folder.Name.ToString, (file.Length / 1024).ToString + "kb")
Me.dgFiles.Rows(dgFiles.Rows.Count - 2).DefaultCellStyle.BackColor = colorOff
Else
dgFiles.Rows.Add(True, file.Name.ToString, folder.Name.ToString, (file.Length / 1024).ToString + "kb")
Me.dgFiles.Rows(dgFiles.Rows.Count - 2).DefaultCellStyle.BackColor = colorOn
End If
Next
Next