introduction
What is a shortcut
- The file with a small arrow in the lower left corner that we click on on the window desktop is the shortcut, and everyone is familiar with it.
- Shortcuts are a method provided by Windows to quickly start programs and open files or folders. It is a quick link to applications or folders, files. The general extension of the shortcut is *.lnk. Its purpose is to provide users with shortcuts to access other files, folders, applications or websites. It is not an executable subject itself, but just a link or reference to another location.
What is a hard link file
- In Windows systems, Hard Link is a connection method in a file system. It is the way to associate a file with another file or directory so that multiple paths point to the same file. This means that any hard link can be regarded as a homologous file of the source file.
Hard links are different from shortcuts
- A shortcut contains a path to the target file or directory. A source file can have multiple shortcuts. Deleting any one or more shortcuts has no effect on the source file.
- The hard link directly adds the target file to the file system directory, and its inode is the same as the original file. Hard links share the same data and file content as the original file, i.e. synchronize content. Actions on one of them will be reflected on the hard link. Deleting any hard link does not affect the original file itself. The disk space occupied by the file will be freed only when all hard links and original files are deleted. Hard links can only be connected to files in the same file system. If multiple process threads are simultaneously viewing and modifying the same problem, using hard links is a good solution.
–>
Let's use C# to create shortcut files!~~
Implement creating shortcut files
The IWshShortcut class comes to the namespace IWshRuntimeLibrary.
The code is as follows:
/// <summary> /// Create shortcut file /// </summary> /// <param name="directory">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, format is "executable or DLL path, icon number", /// For example + "\\" + ", 165"</param> /// <remarks></remarks> public static void CreateShortcut(string directory, string shortcutName, string targetPath, string description, string iconLocation) { //The folder does not exist, create it if (!(directory)) { (directory); } string shortcutPath = $"{directory}\\{shortcutName}.lnk"; WshShell shell = new WshShell(); IWshShortcut shortcut = (IWshShortcut)(shortcutPath);//Create a shortcut object = targetPath;//Specify the target path = (targetPath);//Set the starting position = 1;//Set the operation mode, the state is divided into normal, maximized, and minimized [1,3,7] = description;//Setting Notes = (iconLocation) ? targetPath : iconLocation;//Set icon path ();//Save shortcut }
Implement creating hard link files
Here I use CreateHardLink(), the C++ interface that comes with Windows, which is referenced to the system dynamic library "".
The code is as follows:
[DllImport("", CharSet = )] public extern static bool CreateHardLink (string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes); /// <summary> /// Create hard link files /// </summary> /// <param name="linkNamePath">link path</param> /// <param name="sourceNamePath">Source file path</param> public static bool CreateHardLinkFile(string linkNamePath,string sourceNamePath) { bool result = false; // Delete the target file (if it exists) if ((linkNamePath)) { (linkNamePath); } try { //Create hard link file, set the handle to 0 result = CreateHardLink(linkNamePath, sourceNamePath, ); } catch(Exception ex) { ("CreateHardLinkFile error " + ()); } if (result) { ($"{linkNamePath}Hard link creation successfully!"); } else { ($"{linkNamePath}Hard link creation failed!"); } return result; }
summary
Whether it is shortcuts or hard link files, there are application scenarios suitable for them in Windows and Linux, and see how we use it to serve our programs.
This is the article about how to create shortcut files and hard link files in C#. For more related contents of C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!