Opening a text file by double clicking

Annorax

Member
Joined
Aug 31, 2007
Messages
12
Location
MA, USA
Programming Experience
1-3
Hi everyone,

I am writing a simple text editor. I would like the ability to be able to right click on a text file in my system and "Open With" using my program. Here is what I have so far:

VB.NET:
If Environment.GetCommandLineArgs() Is Nothing Then
	'Make new file normally
Else
	'Open each file in Environment.GetCommandLineArgs() string array
End If

However, this yields in an error:

Exception System.NullReferenceException was thrown in debuggee:
Object reference not set to an instance of an object.

Is this the best approach at doing this? I don't want to associate it with my program, just open it. I also noticed that in the Windows "Open With" list, my icon is there but my program name is blank. Is there a way to set this?

Thanks for any help. :)
 
For future reference, I've managed to figure this out. You need to do the following:

VB.NET:
' Check if the user is opening a file upon startup
If System.Environment.GetCommandLineArgs(0) = "" Then
	Me.mnu_New_Click(Me.mnu_New, New System.EventArgs)			
Else
	Dim s As String
	For Each s In System.Environment.GetCommandLineArgs
		openFile(s)
	Next
End If
 
The first item in the array is normally the path to the executable, so check if array length is larger than 1 instead, this will tell you if command-line arguments was actually supplied.
 
Thanks for the tip, I actually just discovered that. :)

Here's a reworked solution:

VB.NET:
If System.Environment.GetCommandLineArgs().Length > 1 Then
	Dim i As Integer
	For i = 1 To System.Environment.GetCommandLineArgs().Length - 1
		openFile(System.Environment.GetCommandLineArgs(i))
	Next
Else
	Me.mnu_New_Click(Me.mnu_New, New System.EventArgs)
End If

I do have some questions though:

1.) When I right click to "Open With" and add my program, the icon is there but the title is blank. I recently got this fixed but I'm not sure how. I think it might have to do with AssemblyInfo.vb but I'm not sure.
2.) I loop through all command line args, but if I highlight several text files and "Open With", it only opens one. Is this as designed and is the multiple command line args meant more for the "real" command line?
 
I haven't tested this, but not sure what you mean by "opens", what does the args tell you when you do this? what strings does the array contain?
 
Back
Top