[Tut] File Association

Posts 16 of 6 · Page 1 of 1
[Tut] File Association
Hi !

This tutorial will demonstrate you how to associate an extension with your application and reading its contents.

Basically there are two ways of doing this. 1st is through API but that is pretty much advanced. 2nd way of doing this is using registry to simply create an extension for your program in the root class and adding the arguments for it. So we will use registry since it does the same as API.

Let's start:

1: Create a new project.

2: Go to code of the main form.

3: Create a sub-procedure named "AddToRegistry" that can accept 1 parameter of String data type. Add the following code in it:

[php]My.Computer.Registry.ClassesRoot.CreateSubKey("." & extension).SetValue("", extension, Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey(exte nsion & "\shell\open\command").SetValue("", Application.ExecutablePath & " ""%l"" ", Microsoft.Win32.RegistryValueKind.String)[/php]

Where "extension" is the parameter the sub-procedure will accept.

What the procedure does is, it creates a new key for your extension. Then it creates sub keys for it that windows actually recognizes. That are: "Shell", "Open" and "Command" respectively.

Inside the command we set the default value to the path of our application's executable file.

Now we need at least one parameter that our application can read. To read the contents of a file, you can use windows predefine parameter "%l". What it does is that it returns the address of the file you've just opened. So when your application loads it, it can read its contents successfully.

If you want more than 1 parameter, just give a space and add it in a double quote.

4: Now double click the Form to go to the Form's load event. Inside the form's load event enter the following code:

[php]'This will associate the entention to your application.
AddToRegistry()

'Here you catch the arguments you just specified...
For Each Argument As String In My.Application.CommandLineArgs


Dim ReadContent As String = My.Computer.FileSystem.ReadAllText(Argument)


'Just read it and display it in a textbox or message box.
rtb.Text = ReadContent

'If you just want to read the contents then exit Loop
Exit For

Next
[/php]
Now write some sample text in notepad and save it as your extension.
Now run the code (Application). And then close it.
Now double click the new file you created and it will open in your application and also will read everything written in it.

That's all you need to associate the application.

Hope you find it useful.
thanks for sharing this tutorial.
Thanks for sharing, Looks familiar or similar to a article I have seen,)code project I think) But good job
Quote Originally Posted by NextGen1 View Post
Thanks for sharing, Looks familiar or similar to a article I have seen,)code project I think) But good job
Ya I learned it from Codeproject's article. So, I wrote a tutorial

It's originally written by: Nickr5
Yeah, I knew it looked familiar... I have a connection with code project creators, I have pre-looked over so many articles
Quote Originally Posted by NextGen1 View Post
Yeah, I knew it looked familiar... I have a connection with code project creators, I have pre-looked over so many articles
Lol Ya, same here...I've followed tons of CodeProj articles and they alwayz proved of great help to me !!
Posts 16 of 6 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Need help?