Purpose: to generate shortcut startup self startup
Methods: generate shortcuts to the system startup directory and registry method
Difference: the latter may be intercepted or prompted by security software, requiring registry modification permission, etc.
Creating a shortcut in Windows is very simple. You can directly right-click a file or folder and select Create Shortcut. If you want to create it in C# code, it is not so convenient because NET framework does not provide a method to create shortcuts directly.
First, let's see what the shortcut is. Right click the shortcut and select the Properties menu. In the General Tab of the pop-up Properties dialog box, you can see that the file type is shortcut (. lnk), so the shortcut is essentially lnk file.
- Name: the text behind the icon, the file name of the shortcut
- Target type: the type of target that the shortcut points to
- Target location: this shortcut points to the name of the parent folder of the target
- Target: the shortcut points to the full path of the target.
- Start location: the shortcut points to the full path of the parent folder of the target.
- Shortcut key: you can set a shortcut key to open the shortcut. The shortcut key is a combination of Ctrl, Alt, Shift and letter keys.
- Operation mode: the window size after opening the target through this shortcut.
- Note: the note information of the shortcut will be displayed when the mouse hovers over the shortcut.
- Using C # to create a shortcut is to create an lnk file and set related properties NET framework itself does not provide methods, so IWshRuntimeLibrary needs to be introduced. Search for Windows Script Host Object Model in the Add Reference dialog box, select it and add it to the reference of Project. If you need to get the properties of the shortcut, you can call the CreateShortcut method of the WshShell object, and pass in the complete shortcut file path to get the iwshshortcut entity of the existing shortcut. Modify the properties of the shortcut, modify the properties of the IWshShortcut entity, and then call the Save method. using Microsoft.CSharp;// Microsoft. Com under the reference framework CSharp (otherwise, it will prompt that the member Microsoft.CSharp.RuntimeBinder.Binder.Convert required by the compiler is missing);
get ready:
1) Generation class and application considerations
/// <summary> ///Create a shortcut class /// </summary> /// <remarks></remarks> public class ShortcutCreator { //IWshRuntimeLibrary (COM type library) needs to be introduced to search for Windows Script Host Object Model /// <summary> ///Creating shortcuts using IWshRuntimeLibrary; /// </summary> ///< param name = "directory" > the folder where the shortcut is located < / param > ///< param name = "shortcutname" > shortcut name < / param > ///< param name = "targetpath" > target path < / param > ///< param name = "description" > description < / param > ///< param name = "iconlocation" > icon path, in the format of "executable file or DLL path, icon number", ///For example, system Environment. SystemDirectory + "\" + "shell32.dll, 165"</param> /// <remarks></remarks> public static void CreateShortcut(string directory, string shortcutName, string targetPath, string description = null, string iconLocation = null) { if (!System.IO.Directory.Exists(directory)) { System.IO.Directory.CreateDirectory(directory); } string shortcutPath = Path.Combine(directory, string.Format("{0}.lnk", shortcutName)); WshShell shell = new WshShell(); IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);//Create a shortcut object (you must reference Microsoft.CSharp under the framework) shortcut.TargetPath = targetPath;//Specify destination path shortcut.WorkingDirectory = Path.GetDirectoryName(targetPath);//Set start position shortcut.WindowStyle = 1;//Set the operation mode, which defaults to the general window shortcut.Description = description;//Set notes shortcut.IconLocation = string.IsNullOrWhiteSpace(iconLocation) ? targetPath : iconLocation;//Set icon path shortcut.Save();//Save shortcut } /// <summary> ///Create desktop shortcut /// </summary> ///< param name = "shortcutname" > shortcut name < / param > ///< param name = "targetpath" > target path < / param > ///< param name = "description" > description < / param > ///< param name = "iconlocation" > icon path in the format of "executable file or DLL path, icon number" < / param > /// <remarks></remarks> public static void CreateShortcutOnDesktop(string shortcutName, string targetPath, string description = null, string iconLocation = null) { string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//Get desktop folder path CreateShortcut(desktop, shortcutName, targetPath, description, iconLocation); } }
Note: 2 DLLs must be referenced, otherwise a problem will be prompted
using IWshRuntimeLibrary;//IWshRuntimeLibrary needs to be introduced. In the Add Reference dialog box, search for (COM > > type library) Windows Script Host Object Model, select it and add it to the reference of Project. using Microsoft.CSharp;//Microsoft.CSharp otherwise, it will prompt that the member required by the compiler is missing CSharp. RuntimeBinder. Binder. Convert
2) Knowledge points:
How to get file name and file name without extension
string filename = Application.ExecutablePath.Substring(Application.StartupPath.Length + 1);//Remove slash and path, file name with extension string truefilename = getExeName(true);//File name without extension
filename file name with extension
truefilename file name without extension
Pay attention to whether there is shortcut in load, and select the menu if it exists (only judge method 1, and modify method 2 to judge if it exists)
string path = Application.ExecutablePath;//route RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); var isexist=rk2.GetValue(filename);//Boot detection can be read in this way if(isexist!=null)That means it exists rk2.Close(); rk.Close();
If you don't want to be so troublesome, you can judge whether it exists during generation. (9i) introduction to Internet of things browser https://blog.csdn.net/uaime/article/details/117172156?spm=1001.2014.3001.5501)