Open With... help

bfsog

Well-known member
Joined
Apr 21, 2005
Messages
50
Location
UK
Programming Experience
5-10
Lets say you have a text file, on a windows based OS. You right click it and choose Open With, and choose a text editor. The text editor opens with the given file.

I created a text editor, but when I done the above the editor opens but the file is not.

Anyone know how this is done?

Thanks in advance
 
Well, no not really. Whst I want is, to go right click -> Open With -> my texteditor
Then, my text editor opens with that files content.
 
You'd have to register your application to associate with a file extension on the system. You can then configure a menu option such as Open With...and your app. Pass in a %1 which is the file name and trap the command line arguments in your Sub Main() as an example. I do this with my apps via InstallShield's installation system. It is a bit complex and takes some research and testing to ensure you get it right.
 
Oh i see ... i guess you have missed the part that reads the text from certain text file ... so, you'll have to add some code that will do that. For instance, using StreamReader class"

in Load event put this code:

PHP:
 Dim EntireFile AsString 
 
Dim oFile As System.IO.File
 
Dim oRead As System.IO.StreamReader
 
oRead = oFile.OpenText("C:\test.txt") 'test.txt file can be any other file actually any file you have choosen through "Open With Dialog"
 
EntireFile = oRead.ReadToEnd()
 
TextBox1.Text = EntireFile

i hope now you have a clearer picture ... Cheers ;)
 
I have a question to add to this if I may. Seems you all know how to do this. How do I associate a certain file extension with my application. For example.

When you double click on a .txt file, it opens with the default text editor such as notepad. Or when you double click an .mp3 file it opens with the default player.

How do I associate a certain file extension to my program?

Once I associate it, how do I know what code will run first in my program when they double click the file?
 
Ok, I figured out how to start the application with my program by associating the file during the install process. I just let VS do it for me for now, but I would like to learn how to do it programmatically so that I can give the user options. Any how, Now what I need is the second question. The program opens but how do I tell it to use the file that opened it? For example.

When I double click an .mp3 file and the program opens, what routine is now being ran? I don't understand how to start the program with that particular file.
 
Thanks ashishnaicker but that's far from what I am talking about. I am talking about the actual file in windows explorer. When you double click a file it usually is associated with a program and that program will automatically run and operate with that file.

With ALOT of help from the people here I have been very successful at doing this and I understand quite a bit of how to manipulate the program by associating it with a particular file type. I am still looking as to how I can ask the user if they want my program to be the Default Program for the file extension as opposed to just making it that way on install.
 
bfsog said:
Lets say you have a text file, on a windows based OS. You right click it and choose Open With, and choose a text editor. The text editor opens with the given file.

I created a text editor, but when I done the above the editor opens but the file is not.

Anyone know how this is done?

Thanks in advance

If you look in windows, at e.g. the file association for a Text file, in the entry that is for "Open With Notepad" you will see the command line:

Notepad.exe %1



it's the %1 that windows will populate with the full path of the selected file. you can then get this from the argument in your app. If your app has a Module with a Sub Main() that is that startup then you write it like this:

VB.NET:
Public Sub Main(args as String())
 
  MessageBox.Show(args.Length & " number of args were passed to me and the first arg is: " & arg(0)
 
End Sub

compile that and run it from dos:

myapp.exe "hello world"


you will see "1 arg was passed to me and the first arg is: hello world"




so now when you associate it with your .txt file, and you put %1 in to the command line . heres a pic telling my 100 words :)
 

Attachments

  • untitled.GIF
    untitled.GIF
    53.1 KB · Views: 51
Thanks cjard. I go that much covered, I think you missed my last couple of posts, lol. What I am trying to do now is assign the file type to my program programmatically. I have figured out how to use the Project Installer to do it and been very successful but I still want the flexability to manipulate this in my code. Say for example the user chooses another default program then wants to reset my program as the default. Right now they would have to re-install my app to make that happen and I know that's far from modern so I need to modernize my crap asap.
 
O yeah. I remember now. I had forgotten that site had both methods. Thanks :) I will be doing a lot of work with the hard way now to learn it up. :)
 
Back
Top