public class Dlls { [DllImport("shfolder.dll", CharSet = CharSet.Auto)] private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath); private const int MAX_PATH = 260; private const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019; public static string GetAllUsersDesktopFolderPath() { StringBuilder sbPath = new StringBuilder(MAX_PATH); SHGetFolderPath( IntPtr.Zero, CSIDL_COMMON_DESKTOPDIRECTORY, IntPtr.Zero, 0, sbPath); return sbPath.ToString(); } [DllImport("shell32.dll", CharSet = CharSet.Unicode)] private static extern uint SHGetKnownFolderPath(ref Guid rfid, uint dwFlags, IntPtr hToken, out StringBuilder path); public static string GetSharedDesktop() { Guid FOLDERID_PublicDesktop = new Guid(0xC4AA340D, 0xF20F, 0x4863, 0xAF, 0xEF, 0xF8, 0x7E, 0xF2, 0xE6, 0xBA, 0x25); StringBuilder path = new StringBuilder(260); uint retval = SHGetKnownFolderPath(ref FOLDERID_PublicDesktop, 0, IntPtr.Zero, out path); return path.ToString(); } } private static void CreateDesktopLnk() { string DesktopPath = ""; int osVer; string desktopFolder; OperatingSystem os = Environment.OSVersion; osVer = os.Version.Major; if (osVer < 6) { DesktopPath = Dlls.GetAllUsersDesktopFolderPath(); } else { DesktopPath = Dlls.GetSharedDesktop(); } string oldlink = Path.Combine(DesktopPath, "XXXXX.lnk"); DeleteFile(oldlink); IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass(); IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(DesktopPath + "//XXXXXX.lnk"); string strExec = System.Reflection.Assembly.GetExecutingAssembly().Location; strExec = Path.GetDirectoryName(strExec); shortcut.TargetPath = Path.Combine(strExec, "OrderGenerater//GetXMLData.exe"); shortcut.Arguments = "";// 参数 shortcut.Description = "XXXXXX"; shortcut.WorkingDirectory = Path.Combine(System.Environment.CurrentDirectory, "XXXXXX//"); shortcut.IconLocation = shortcut.TargetPath + ",0";//图标 shortcut.Hotkey = ""; shortcut.WindowStyle = 1; shortcut.Save(); }