• 0

File association in C#


Question

6 answers to this question

Recommended Posts

  • 0

I would just write the app name/location to registry

ie;

HKEY_CLASSES_ROOT\txtfile.txt (.rtf etc..)

use the Microsoft.Win32.RegistryKey class

string keyName;

string keyValue;

//This creates a Root entry called "MyEditor".

keyName = "MyEditor";

keyValue = "My Editor Application";

RegistryKey key;

key = Registry.ClassesRoot.CreateSubKey(keyName);

key.SetValue("",keyValue);

key = key.CreateSubKey("shell");

key = key.CreateSubKey("open");

key = key.CreateSubKey("command");

key.SetValue("","c:\\temp\\Editor.exe %1");

keyName = ".txt";

keyValue = "MyEditor";

key = Registry.ClassesRoot.CreateSubKey(keyName);

key.SetValue("", keyValue);

  • 0
  jojoey said:
...thanks...and how can I make my App read as example the txt file when this

registry thing is done?

Example:

-Click joe.txt

-Open MyEditor

-Read the file

585348795[/snapback]

Your app would have to have a command line parser that would read your arguments, and then load the file. If your Main method doesn't already have an arguments parameter, just add string[] args.

static void Main(string[] args){}

http://msdn.microsoft.com/library/default....erstutorial.asp

  • 0
  jojoey said:
All I need?

...before I do all the work...is there any complete sample...?

585350421[/snapback]

The shell handles the tokenization of your command-line.

key.SetValue("","c:\\temp\\Editor.exe %1");

the %1 is the token for your command-line argument. So, if you dbl-click on a file in Explorer, the shell will call your editor, passing the filename clicked upon as an argument.

You need to use the System.IO tools to open the file as text(or whatever), and read the lines into your editor. You may want to go through some tutorials for File I/O.

http://msdn.microsoft.com/library/default....rclasstopic.asp

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.