Hi
I'm working on an application that loads a text file into a data grid.
So far I have made it possible to Open it using standard file dialog.
What I would like to do is to open these files by doubel clicking on them directly in Windows. I have tried several suggested ways but can not get any of them to work with my code. I'm using VB2008.
Here is my current dialog opening procedure.
I'm working on an application that loads a text file into a data grid.
So far I have made it possible to Open it using standard file dialog.
What I would like to do is to open these files by doubel clicking on them directly in Windows. I have tried several suggested ways but can not get any of them to work with my code. I'm using VB2008.
Here is my current dialog opening procedure.
VB.NET:
Dim myStream As System.IO.Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
Dim n As Integer = Nothing
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
Dim objReader As New System.IO.StreamReader(myStream)
Dim fileText(1000) As String
Do While objReader.Peek() <> -1
fileText(n) = objReader.ReadLine()
If n > 1 Then
fileText(n) = fileText(n).Replace(" ", ",")
End If
n = n + 1
Loop
Dim i, DotPosition As Integer
DotPosition = InStr(fileText(2), ",,")
For i = 2 To 1000
Do Until InStr(fileText(i), ",,") = 0
fileText(i) = fileText(i).Replace(",,", ",")
fileText(i) = fileText(i).Replace(",,,", ",")
fileText(i) = fileText(i).Replace(",,,,", ",")
fileText(i) = fileText(i).Replace(",,,,,", ",")
fileText(i) = fileText(i).Replace(",,,,,,", ",")
Loop
Next i
Dim p, q As Integer
Dim aryTextFile() As String = Nothing
Dim textLine
p = 1
For q = 2 To 1000
textLine = fileText(q)
If textLine <> "" Then
aryTextFile = textLine.Split(",")
Else
q = 1000
End If
row(p) = aryTextFile(1)
row(p + 1) = aryTextFile(2)
row(p + 2) = aryTextFile(3)
row(p + 3) = aryTextFile(4)
row(p + 4) = aryTextFile(5)
With Me.DataGridView1.Rows
.Add(row(p), row(p + 1), row(p + 2), row(p + 3), row(p + 4))
End With
p = p + 5
Next q
IregInfoStatus.Text = fileText(0)
objReader.Close()
myStream.Close()
End If
Catch Ex As Exception
MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
' Check this again, since we need to make sure we didn't throw an exception on open.
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If