C # Settings custom file icon Implementation Double click Start (Modify registry) _c# Tutorial

Source: Internet
Author: User

The custom file generated by the program, such as the suffix is . Test

How does this file directly start the open program and open the file?

1. Double-click to open

2, the custom file, has the icon to display

3, the custom file, right click has the corresponding attribute

Background code: (How to modify information in the registry)

  Tool startup path String toolpath = System.Windows.Forms.Application.StartupPath + \ Mail gadget. exe;

  string extension = Sptdconst.fileextension;

  String fileType = "Email File";
  String filecontent = "Text/plain";

  Get information Microsoft.Win32.RegistryKey RegistryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey (extension); if (RegistryKey!= null && registrykey.opensubkey ("shell")!= null && registrykey.opensubkey ("Shell"). OpenSubKey ("open")!= null && registrykey.opensubkey ("Shell"). OpenSubKey ("open"). OpenSubKey ("command")!= null) {var varsub = Registrykey.opensubkey ("Shell"). OpenSubKey ("open").
    OpenSubKey ("command");

    var varvalue = Varsub.getvalue ("");
    if (Object.Equals (varvalue, Toolpath + "%1")) {return;
  }//delete Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree (extension, false);
  File Registration RegistryKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey (extension);
RegistryKey.SetValue ("File type", FileType);  RegistryKey.SetValue ("Content Type", filecontent);
  Set default icon Microsoft.Win32.RegistryKey Iconkey = Registrykey.createsubkey ("DefaultIcon");
  Iconkey.setvalue ("", System.Windows.Forms.Application.StartupPath + "\\logo.ico");
  Set the default open program path RegistryKey = Registrykey.createsubkey ("Shell\\open\\command");
  RegistryKey.SetValue ("", Toolpath + "%1"); Close Registrykey.close ();

After modifying the registry information, double-click the file to start the software, then how to operate in the code?

Double-click Start
 on//If there are spaces in the existing path, it will be decomposed into multiple elements
if (e.args.length > 0)
{
   string filePath = String.Join ("", E. Args.toarray ());
   FileInfo file = new FileInfo (filePath);
   if (file. Exists)
    {
      Emailtoolconst.doubleclicksptdfilepath = file. FullName
    }
 }

You can then determine if there is a value in the main program method, and if so, loaded DoubleClickSptdFilePath get the file under the path to continue.

The above is a small series for everyone to organize the C # set up a custom file icon to achieve the start of the whole content, I hope this article content for everyone's study or work can have some help, if there is doubt can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.