Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file with association

i made a file editor in C#, and i can open files using the 'Open' button in the toolbar, i also associated the correct file types to the program, so when i click a file with the extension *.nlp the program opens correctly, but does not open the file itself (which is quite logical since i did not implement it yet)

now is my question, how do i implement such a thing? i want the file to be opened and loaded when i click on it.

(btw, the file is just plain text, so nothing special, and it's for windows if that matters)

like image 746
Nick Avatar asked Jun 05 '12 06:06

Nick


People also ask

How do I force a file to open with a specific program?

From the desktop, right-click the desired file, select Open with, and click Choose another app from the menu that appears. Select the desired application. If you don't see the one you want, click More apps or Look for an app in the Store to look for other applications.

How do I set file associations in Windows 10?

In Windows 7, Windows 8, and Windows 10, choose Start and then type Control Panel. Choose Programs > Make a file type always open in a specific program. If you don't see Programs, choose Default Programs > Associate a file type or protocol with a program.


3 Answers

In windows file associations are stored and managed in the registry under HKEY_CLASSES_ROOT

You can do the following manually or eventually write a little setup program to write the correct entries to the registry.

You need to register your extension and then associate it with a program like this document describes. Also see this doc Your registry should look like this:

HKEY_CLASSES_ROOT
   .nlp
      (Default) = YourProgID//can by anything you want
   YourProgID
      shell
         open
            command
               (Default) = yourapp.exe %1

Now, they key to your answer is the %1 in the command key. It is the filename that was opened and it passed as an argument to you app.

So :

static void Main(string[] args)
{
   // args will contain your filename
}
like image 195
gideon Avatar answered Sep 28 '22 02:09

gideon


There doesn't appear to be a .Net API for directly managing file associations but you can use the Registry classes for reading and writing the keys you need to.

You'll need to create a key under HKEY_CLASSES_ROOT with the name set to your file extension (eg: ".txt"). Set the default value of this key to a unique name for your file type, such as "Acme.TextFile". Then create another key under HKEY_CLASSES_ROOT with the name set to "Acme.TextFile". Add a subkey called "DefaultIcon" and set the default value of the key to the file containing the icon you wish to use for this file type. Add another sibling called "shell". Under the "shell" key, add a key for each action you wish to have available via the Explorer context menu, setting the default value for each key to the path to your executable followed by a space and "%1" to represent the path to the file selected.

For instance, here's a sample registry file to create an association between .txt files and EmEditor:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.txt]
@="emeditor.txt"

[HKEY_CLASSES_ROOT\emeditor.txt]
@="Text Document"

[HKEY_CLASSES_ROOT\emeditor.txt\DefaultIcon]
@="%SystemRoot%\\SysWow64\\imageres.dll,-102"

[HKEY_CLASSES_ROOT\emeditor.txt\shell]

[HKEY_CLASSES_ROOT\emeditor.txt\shell\open]

[HKEY_CLASSES_ROOT\emeditor.txt\shell\open\command]
@="\"C:\\Program Files\\EmEditor\\EMEDITOR.EXE\" \"%1\""

[HKEY_CLASSES_ROOT\emeditor.txt\shell\print]

[HKEY_CLASSES_ROOT\emeditor.txt\shell\print\command]
@="\"C:\\Program Files\\EmEditor\\EMEDITOR.EXE\" /p \"%1\""

Credit to @X-Cubed

like image 37
Faraday Avatar answered Sep 28 '22 03:09

Faraday


The filename will be passed as an argument to your application:

public static void Main(string[] args)
{
  if ( args != null && args.Length > 0 )
  {
    string filename = args[0];
    if ( File.Exists ( filename ) )
    {
      //Open file 
    }
  }
}
like image 35
Stephan Bauer Avatar answered Sep 28 '22 03:09

Stephan Bauer