Associating File Types

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I associate file types with my application, and use its icon? Also is there an event I can use for when my application is loaded by opening those filetypes?
Ex. Open a .rtf, and I want my form to open that .rtf in its textcontrol.
 
Okay I figured out how to associate file types, now is there an event I can use to handle my Application being run by opening a file.
 
Hi, Bill

not very clear what do you mean.
However, if your app is associated with file, I expect it will get some kind
of parameter(s). So, possibly simple check if arguments are there could
help?

HTH
Alex

Bill English said:
Okay I figured out how to associate file types, now is there an event I
can use to handle my Application being run by opening a file.
 
Sorry... Don't know what a parameter is. But for example when you double click on a file with a .txt extension, notepad opens it. If I associate .rtf with my program, I want it to read the .rtf file, then write its contents to the text control in my form. The only thing is I need an event to trigger this to happen

----- AlexS wrote: ----

Hi, Bil

not very clear what do you mean
However, if your app is associated with file, I expect it will get some kin
of parameter(s). So, possibly simple check if arguments are there coul
help

HT
Ale

Bill English said:
Okay I figured out how to associate file types, now is there an event
can use to handle my Application being run by opening a file
 
Your filename will be passed as the first argument on the command line.
Check it with something like this:

static void Main(string[] args)
{
if (args.Length == 1 && System.IO.File.Exists(args[0]))
{
Application.Run(new MainWindow(args[0]));
}
else
{
Application.Run(new MainWindow(null));
}
}

....and you can handle loading the file in your application class
constructor.

-Jason
 
Could you please build on this? I don't understand how I would go about handling this at all. I don't even understand what you wrote, but PLEASE explain it... I need this.
 
OK Bill, calm down.

I'm assuming from your previous post that you figured out how to
associate a file type in Windows, so now you can double-click a file and
it runs your application, yes?

What's happening, is that Windows is effectively running your
application as if you had typed:

C:\>YourApp.exe foo.rtf

....on the command line. It's telling you the name of the file that was
clicked by passing it to you as a command line argument.

What you need next, is the code to figure out what command line
arguments have been passed to you. That's what I gave you in my previous
post. When your program's main() function is called, it is passed a
string array containing whatever arguments were on the command line when
your executable was invoked:

static void Main(string[] args)

That's what 'args' is. Next we check how many arguments there were, and
if there was just one, we test to see if it's a file that exists on the
file system somewhere:

if (args.Length == 1 && System.IO.File.Exists(args[0]))

args[0] in this case would be the string "foo.rtf".

Now we know what file was clicked to launch our program, and from here
we can do whatever we want with that file. I'm assuming we have a Form
called MainWindow that represents our application (substitute your own
appropriate class name here, Form1 perhaps), and so I pass the filename
to the constructor of MainWindow when I start the app:

Application.Run(new MainWindow(args[0]));

The rest was left up to your imagination, which I had assumed would be
able to recognize that you could create a constructor like:

public MainWindow(string filename)
{
// here's where you would do something with the
// filename, like open the file and read the
// contents into your control
}

I can't walk you through the rest of the details right now, but perhaps
someone else would like to take a turn.

-Jason
 
Back
Top