Open files from certain extension on my program

Asked

Viewed 439 times

2

I’m making an application in Visual Studio, with the intention of it work with own extension files (.rmt, subject to change) and can open it later.

I want that when the user double-click on the files with this extension, the system opens the file using my application, so that it can process the file as I want.

How can I link this extension to my app?

  • and what have you done? What error are you encountering?

  • then @Ricardo , nothing... intended to start there, but maybe exporting this file as a folder to another directory is a viable option. but would like to be able to export and import that folder, to open on another pc, for example.

  • It seems to me that the main question is the association of the file with the program. In this case, if the OP has affinity with English, it has this excellent answer in the Stack Overflow: http://stackoverflow.com/questions/1387769/create-registry-entry-to-associate-file-extension-with-application-in-c would be great for the community.

  • @Pablo just a note, it is good to remember that in Windows 10 changed the way of dealing with file associations. Apparently this was not covered in the given question. For earlier versions, it seems sufficient.

  • @Bacco But even for Win32 applications?

  • start with the "create your own extension file"

  • @Pablo yes, with the 10 MS does not want the program to override by its own window, but by a standard OS window, at the end of the installation

  • @Bacco Sacked. It seems that the work to answer this question will be even greater. =/

  • @vhoyer when saving the file, you can use the extension you want. This does not change anything. The problem is to associate this extension with your program, which is Pablo’s appointment. By clicking on the program, once associated, your program will receive the file name as if your program had been called: "programaDoVhoier.exe meuarquivo.rmt". I think you should focus the question on the association.

  • well then, first it would have to open a compressed file like zip, but with another extension name?

  • @vhoyer your program already compact and unzip the desired format? This would be a 4th problem :) - Remember that you should ask separate questions for each part of the problem, otherwise it gets complicated. This site is not a forum, but a site of objective questions that receive objective answers. And you can ask one question at a time, and ask new questions at will, as you solve each step. As long as the questions fit the format of the site, you can do several.

  • I understand, sorry if I had not understood the format of the site right... I will try not to post this kind of thing anymore, so for now, I would have a suggestion to fix this question? and not yet, because when I started thinking about it I came to the stack, but for everything I read before asking the question, it’s relatively easy to compress and unpack, so it wouldn’t be a "problem"

  • @Bacco then I think I’ll remove the question and create others, thanks for the help and sorry for the inconvenience...

  • @vhoyer took the liberty of editing your question by focusing on the association of the files, but if you don’t like how it looks, you can click here and choose the Revert option in its previous version (5) to discard the changes.

  • No, thanks @Bacco I was going to try to edit, but you went faster

  • @vhoyer feel free to edit over and leave with your words if you feel it necessary. I only tried to leave a single problem in the question, even to make it easier for those who answer. And the good news is, with separate questions, the answers can not only help you, but can be found more easily by others with the same problem. And being separated, people with different knowledge can help better in the parts that dominate.

Show 11 more comments

1 answer

1


The Own Clickonce creates extensions, but you can also create a key in the registry to associate a file extension to your executable. First, declare this static method:

public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
{
   RegistryKey BaseKey;
   RegistryKey OpenMethod;
   RegistryKey Shell;
   RegistryKey CurrentUser;

   BaseKey = Registry.ClassesRoot.CreateSubKey(Extension);
   BaseKey.SetValue("", KeyName);

   OpenMethod = Registry.ClassesRoot.CreateSubKey(KeyName);
   OpenMethod.SetValue("", FileDescription);
   OpenMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + OpenWith + "\",0");
   Shell = OpenMethod.CreateSubKey("Shell");
   Shell.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
   Shell.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + OpenWith + "\"" + " \"%1\"");
   BaseKey.Close();
   OpenMethod.Close();
   Shell.Close();

   CurrentUser = Registry.CurrentUser.CreateSubKey(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ucs");
   CurrentUser = CurrentUser.OpenSubKey("UserChoice", RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl);
   CurrentUser.SetValue("Progid", KeyName, RegistryValueKind.String);
   CurrentUser.Close();
}

Ai, you can programmatically associate your application using this method:

SetAssociation(".rmt", "Nome_da_extensao", Application.ExecutablePath, "Minha extenção .rmt");

I got this response from this link here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.