Opening file automatically in data grid

Deloma

New member
Joined
Nov 21, 2008
Messages
2
Programming Experience
Beginner
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.

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
 
If you want your app to open when a file is double-clicked in Windows Explorer then you must first set up a file association. If you create a Setup project then that can be created when you app is installed. Otherwise you can do it manually in the Folder Options dialogue. You can also do it in code by manipulating the Registry but I don't know the specifics.

Actually making use of the file when your app opens is a completely separate matter. You would either handle the Startup event of your app and get the e.Args property or else call Environment.GetCommandLineArgs anywhere in your code, which would most likely be the Load event handler of the main form.
 
Thanks for the reply. Assigning the file is not a problem. I'm more interested in how to handle it in Load event section with Environment.GetCommandLineArgs
Right now when I click on these files my program Starts but nothing is loaded.
I just don't know how to use GetCommandLineArgs properly so my file gets loaded.
 
This code:
Hi



VB.NET:
                        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

Would be better done as:

Regex.Replace(fileText(i), ",+", ",");
 
Thanks for the reply. Assigning the file is not a problem. I'm more interested in how to handle it in Load event section with Environment.GetCommandLineArgs
Right now when I click on these files my program Starts but nothing is loaded.
I just don't know how to use GetCommandLineArgs properly so my file gets loaded.

If you choose to have a Sub Main instead of a startup form in your app, then you can write a sub main:

VB.NET:
Public Shared Sub Main(args() as String)
'args is an aray of all cmd line args
End Sub

Your sub main will also need to make a new instance of your main form, and pass it into a call to System.Windows.Forms.Application.Run(<form>)
 
Thanks for the reply. Assigning the file is not a problem. I'm more interested in how to handle it in Load event section with Environment.GetCommandLineArgs
Right now when I click on these files my program Starts but nothing is loaded.
I just don't know how to use GetCommandLineArgs properly so my file gets loaded.
Did you bother to read the documentation for that method? If you had then you'd have seen a code example plus an explanation. You'd know that it returns a string array containing the commandline arguments, so all you need to do is get the file path from that array and then use it to open the file.

If someone tells you what type or member to use then the documentation for that type or member is the first place you should look.
 
Back
Top