C # File Association

Source: Internet
Author: User

Like other languages, C # also needs to directly perform operations on the registry to achieve file association, that is, set the file extension, document type description, friendly name, icon, call method, and other key values according to rules, write the following code on the Internet.

Using Microsoft. Win32;

Registrykey key = registry. classesroot. opensubkey (". JB ");
If (Key = NULL)
{
Key = registry. classesroot. createsubkey (". JB ");
Key. setvalue ("", "jeebook. Reader. JB ");
Key. setvalue ("content type", "application/JB ");

Key = registry. classesroot. createsubkey ("jeebook. Reader. JB ");
Key. setvalue ("", "jeebook document ");

Registrykey keysub = key. createsubkey ("defaulticon ");
Keysub. setvalue ("", system. Windows. Forms. application. startuppath + "jeebook. ICO ");
Keysub = key. createsubkey ("shell // open // command ");
Keysub. setvalue ("", "/" "+ system. Windows. Forms. application. executablepath +"/"/" % 1 /"");
}

Relatively speaking, it is difficult to implement C # by using the icon path. Generally, the file type registers the resources of the execution file, in the following format:

Format:, // The resource ID is a positive number, indicating the index number of the resource. Otherwise, it is the resource ID.

In C ++, it is easy to obtain the resource ID, but C # does not know how to obtain the resource ID. Therefore, an icon file is included in the program execution directory for registration, in this way, it is convenient to modify at any time :)

In addition, it is how to verify whether the icon is set correctly. The current system is so fond of caching that it is difficult to respond immediately after manual modification of the Registry unless it is performed at the log off level. Of course, you should refresh the system status through program calls. Please study it again next time.

The above code is sufficient if it is used only for programs below XP. Unfortunately, UAC will be introduced in Vista later, and the above Code will pop up exceptions under UAC, which is definitely not what we hope.

One solution is to add the following script by creating a mainifest file:








In this way, a small shield will appear on the program icon, indicating that the program needs to run in administrator mode. The UAC verification window will automatically pop up each time it runs. However, for some programs that only need to write the registry once, the prompt is intolerable every time. How can we pop up the UAC verification window through code?

Google's results do not seem to trigger a prompt during running and change the process to administrator mode. However, you only need to set the verb to RunAs when executing the process to automatically trigger UAC verification, enable the called program to run in administrator mode. Then, you only need to restart yourself in the program to trigger the UAC Authentication Window. The Code is as follows:

System. Diagnostics. processstartinfo start = new system. Diagnostics. processstartinfo ();
System. Diagnostics. PROCESS p = new system. Diagnostics. Process ();
Start. workingdirectory = system. Windows. Forms. application. startuppath;
Start. verb = "RunAs ";
Start. filename = system. Windows. Forms. application. executablepath;
System. Diagnostics. process. Start (start );

Of course, if you put the above Code directly into your formload, it will be a disaster, because your program will constantly start itself until your machine resources are exhausted. Therefore, we also need code that can determine whether the Administrator mode is enabled, as shown below:

Windowsidentity Wi = windowsidentity. getcurrent ();
Windowsprincipal Wp = new windowsprincipal (WI );

If (! WP. isinrole (windowsbuiltinrole. Administrator ))

Wi indicates the current user, windowsbuiltinrole. administrator is the administrator's identifier (it is strange that VB seems to have its own special namespace to implement the above functions. I don't know why, of course, we can judge it in a standard way)

By integrating all the above Code, the following results are obtained:

If (null! = Registry. classesroot. opensubkey (". JB "))
Return;

Windowsidentity Wi = windowsidentity. getcurrent ();
Windowsprincipal Wp = new windowsprincipal (WI );

If (! WP. isinrole (windowsbuiltinrole. Administrator ))
{
System. Diagnostics. processstartinfo start = new
System. Diagnostics. processstartinfo ();
System. Diagnostics. PROCESS p = new system. Diagnostics. Process ();
Start. workingdirectory = system. Windows. Forms. application. startuppath;
Start. verb = "RunAs ";
Start. filename = system. Windows. Forms. application. executablepath;
System. Diagnostics. process. Start (start );
Application. Exit ();
Return;
}

Registrykey key = registry. classesroot. opensubkey (". JB ");
If (Key = NULL)
{
Key = registry. classesroot. createsubkey (". JB ");
Key. setvalue ("", "jeebook. Reader. JB ");
Key. setvalue ("content type", "application/JB ");

Key = registry. classesroot. createsubkey ("jeebook. Reader. JB ");
Key. setvalue ("", "jeebook document ");

Registrykey keysub = key. createsubkey ("defaulticon ");
Keysub. setvalue ("", system. Windows. Forms. application. startuppath + "jeebook. ICO ");
Keysub = key. createsubkey ("shell // open // command ");
Keysub. setvalue ("", "/" "+ system. Windows. Forms. application. executablepath +"/"/" % 1 /"");
}

File Association is a very common and simple function, but it takes some effort to do a good job :)

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.