Install and uninstall the WPF program.

Source: Internet
Author: User

Install and uninstall the WPF program.

Preface

Speaking of the installation program, this may be a part of the forgotten part, so doing C/S is a friend, do your programs really do not need a cool installation program?

 

Statement first

This article aims to teach you how to install it in your own way.

 

Analysis

We often install various programs, and the installation steps can be summarized as below:

1. Welcome to the page-No profiling

2. Select the path page-No profiling

3. Installation page-including Decompression and registry Addition

4. Completion page-create shortcuts, start and run upon startup

Modify the settings as needed.

After analysis, we found that a tall installation program really has no technical skills, but it is actually a decompression operation, writing it into the registry !!!

 

Based on the analysis results, we will install the SDK step by step.

 

Extract

Here we use the third-party ICSharpCode. SharpZipLib. Zip to decompress the package,

For specific usage, you can search for it by yourself. Here is a ready-made method.

/// <Summary> /// decompress the file /// </summary> /// <param name = "sourceFile"> source directory </param> /// <param name = "destinationFile"> target directory </param> public void DeCompressFile (string sourceFile, string destinationFile, Action call) {try {if (! System. IO. file. exists (sourceFile) {return;} if (Directory. exists (destinationFile) {Directory. delete (destinationFile, true);} // read the compressed file (zip file) and prepare to decompress ZipInputStream s = new ZipInputStream (System. IO. file. openRead (sourceFile. trim (); ZipEntry theEntry; string path = destinationFile; // The path to save the extracted file: string rootDir = ""; // name of the first sub-folder in the root directory while (theEntry = s. getNextEntry ())! = Null) {rootDir = System. IO. path. getDirectoryName (theEntry. name); // obtain the Name of the first level sub-folder under the root directory if (rootDir. indexOf ("\")> = 0) {rootDir = rootDir. substring (0, rootDir. indexOf ("\") + 1);} string dir = System. IO. path. getDirectoryName (theEntry. name); // Name of the folder under the first level sub-folder under the root directory string fileName = System. IO. path. getFileName (theEntry. name); // the file Name in the root directory if (dir! = "") // Create a sub-folder under the root directory, with no limit on the level {if (! Directory. exists (destinationFile + "\" + dir) {path = destinationFile + "\" + dir; // create a folder Directory in the specified path. createDirectory (path) ;}} else if (dir = "" & fileName! = "") // File {path = destinationFile;} else if (dir! = "" & FileName! = "") // The file in the first level subfolder under the root directory {// specifies the path for saving the file if (dir. indexOf ("\")> 0) {path = destinationFile + "\" + dir ;}} // determine whether to save the file in the root directory if (dir = rootDir) {path = destinationFile + "\" + rootDir ;} // The following are the basic steps to decompress the zip file. // The basic idea is to traverse all the files in the compressed file and create an identical file. If (fileName! = String. empty) {FileStream streamWriter = System. IO. file. create (path + "\" + fileName); int size = 2048; byte [] data = new byte [2048]; while (true) {size = s. read (data, 0, data. length); if (size> 0) {streamWriter. write (data, 0, size) ;}else {break ;}} streamWriter. close () ;}} s. close (); call. invoke ();} catch (Exception ex) {System. windows. messageBox. show (ex. message );}}

 

Registry

I believe some people do not know much about installing the Registry. You can open the control panel-programs and functions to see that all the programs you have installed are here. How did they appear here?

In fact, the application information is added to the Registry SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall. Of course, our program is no exception,

Based on the registration information of other programs, we can find the following nodes:

/// <Summary> /// register application information /// </summary> /// <param name = "setupPath"> installation path </param> public void AddRegedit (string setupPath) {try {RegistryKey = Registry. localMachine. openSubKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall", true); RegistryKey software = key. createSubKey (RegeditKey); // The software Icon. setValue ("DisplayIcon", setupPath + "\" + AppExe); // display name software. setValue ("DisplayName", DisplayName); // version of software. setValue ("DisplayVersion", Version); // program distribution company software. setValue ("Publisher", Publisher); // installation location software. setValue ("InstallLocation", setupPath); // install the source software. setValue ("InstallSource", setupPath); // help call // software. setValue ("HelpTelephone", "123456789"); // uninstall the path software. setValue ("UninstallString", setupPath + "/uninstall.exe ");
Software. Close ();
Key. Close ();
} Catch (Exception ){}
}

You can write your own program information. Here we will focus on the UninstallString. This value is the point of the uninstall program, that is, the program called when you choose to uninstall in the control panel-programs and functions.

In principle, if we have an installation, We need to write an uninstallation Program (used to delete the registry, program directory, and shortcuts). We will not repeat it here.

 

Create shortcuts

/// <Summary> /// create a shortcut /// </summary> /// <param name = "setupPath"> Installation Directory </param> /// <param name = "isDesktop"> whether to create a desktop (default to the installation directory) </param> public void CreateLnk (string setupPath, bool isDesktop) {string path = setupPath; if (isDesktop) {path = Environment. getFolderPath (System. environment. specialFolder. export topdirectory);} // Note: if there is a shortcut key to be created on the desktop, when the program executes the creation statement, the existing shortcut key on the desktop will be modified, and the program will not encounter exceptions WshShell shell = new Wsh Shell (); // the location where the shortcut key is created, and the name is IWshShortcut shortcut = (IWshShortcut) shell. createShortcut (path + "\" + DisplayName + ". lnk "); // destination file shortcut cut. targetPath = setupPath + "\" + AppExe; // This attribute specifies the working directory of the application. If you do not specify a specific directory, the target application of the shortcut uses the directory specified by this attribute to load or save files. Shortcut cut. workingDirectory = setupPath; // System. environment. currentDirectory; // The Window status of the target application is divided into normal, maximized, and minimized [1, 3, 7] // shortcut. windowStyle = 1; // The parameter cut. description = Description; // shortcut icon shortcut cut. iconLocation = setupPath + "\" + AppIco; shortcut cut. arguments = ""; // shortcut cut. hotkey = "CTRL + ALT + F11"; // Save the shortcut cut. save ();}

 

Start

The startup function is simply to add the startup function at the specified location in the registry.

I am calling a shortcut when starting the instance, instead of an exe program.

/// <Summary> /// add boot start/// </summary> /// <param name = "setupPath"> Installation Directory </param> public void AddRun (string setupPath) {try {RegistryKey runkey = Registry. localMachine. openSubKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true); // open the Registry subitem if (runkey = null) // if This item does not exist, create this subitem {runkey = Registry. localMachine. createSubKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run");} // set it to start runkey upon startup. setValue (RegeditKey, "\" "+ setupPath +" \ "+ DisplayName + ". lnk \ ""); runkey. close () ;}catch (Exception ){}}

 

Run

public void RunApp(string setupPath)        {            System.Diagnostics.Process.Start(setupPath + "\\" + DisplayName + ".lnk");        }

After installation, you will think that I only want to get an installed exe program, not the entire file list of the installation program. I have not found a good solution here, this package can only be compressed into one executable program through programs such as WinRar.

 

End

So far, our cool installer is over. How can we improve cool and good? This article does not mention the interface at all ~!

Note that because registry operations are involved, our installer must run as an administrator (you can search for programs running as administrators ).

 

Appendix

If you need a demo, you can leave your email box and send it around a week (there is no demo available now). If you think you can write this article, we recommend it.

Related Article

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.