open text file with application

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I have an app made with vb2010. When I double click the text file I want to open the text file in an application created with vb2010.

I use the following code, but when I try to open the text file with my application, it closes as fast as it opens.
Also the listbox should be enabled when the item count is greater then zero and the filename of the text file should be in the statusbar label.

On form_load
VB.NET:
Dim cla As String() = Environment.GetCommandLineArgs()
        Dim file_line As String
        If cla.Length > 1 Then
            myStreamReader = System.IO.File.OpenText(cla(1))
            While myStreamReader.Peek >= 0
                file_line = myStreamReader.ReadLine
                ListBoxControl1.Items.Add(file_line)
            End While
            myStreamReader.Dispose()
            If ListBoxControl1.Items.Count > 0 Then
                ListBoxControl1.Enabled = True
            End If
        End If

Thanks
 
Is there any reason you're using File.OpenText rather than New StreamReader(path) ? From the MSDN entry, they should work the same, but it might be worth trying the alternative.
 
Hi ud2008,

I always use this:

Try
Using sr As New StreamReader(Fullpath)
Dim line As String
Do
line = sr.ReadLine()
If Not (line Is Nothing) Then
ListBoxControl1.Items.Add(line)
End If
Loop Until line Is Nothing
End Using
Catch
End Try

Works Everytime ;)

Gunze
 
Loading the data into the ListBox can be done far more simply than any of these suggestions:
myListBox.Items.AddRange(IO.File.ReadAllLines("file path here"))
You should also use the File.Exists method to ensure that the path is a valid file before opening it.

The real issue is the fact that the form is closing though. Where exactly is that code?
 
Hello

I just tested your code using test data, obviously the only way I can simulate the commandlineargs is to put the data straight into the array, so I commented out the original to test it:

'Dim cla As String() = Environment.GetCommandLineArgs()Dim cla As String() = {"Default", "Test.txt"}

Test.txt held four lines of test data and these were all put into the listboxcontrol as expected, and it stayed open as it should. What data is being passed to the program. I have seen some very odd behaviour with applications over the years when unexpected data was used.

As far as the filename in the stausbar label, I can't see the code to have this happen.

regards
 
Loading the data into the ListBox can be done far more simply than any of these suggestions:
myListBox.Items.AddRange(IO.File.ReadAllLines("file path here"))
You should also use the File.Exists method to ensure that the path is a valid file before opening it.

The real issue is the fact that the form is closing though. Where exactly is that code?

How can I get the path from the file when I double click a file?
 
You don't get the path FROM the file. You get the path OF the file. It's that path that is in your commandline arguments.

If your form is closing then either you're closing it or something is broken. If you can swear that you're not closing it, I'd suggest zipping up the solution and posting it here to see if the same thing happens for one of us. Make sure you delete all binaries first, which means at least the bin and obj folders.
 
Back
Top