สร้างทางลัดบนเดสก์ท็อป


106

ฉันต้องการสร้างทางลัดที่ชี้ไปที่ไฟล์ EXE บนเดสก์ท็อปโดยใช้. NET Framework 3.5 และอาศัย Windows API อย่างเป็นทางการ ฉันจะทำเช่นนั้นได้อย่างไร?


1
การใช้ Windows Script Host Object Model จาก Rustam Irzaev เป็นวิธีเดียวที่เชื่อถือได้สำหรับทางลัดที่เหมาะสม ayush: เทคนิคนี้ขาดคุณสมบัติหลายอย่างเช่นปุ่มลัดและคำอธิบาย Thorarin: ShellLink ทำงานได้ดีในกรณีส่วนใหญ่ แต่โดยเฉพาะอย่างยิ่งมันไม่ทำงานใน Windows XP และสร้างทางลัดที่ไม่ถูกต้อง Simon Mourier: สิ่งนี้มีแนวโน้มมาก แต่สร้างทางลัดที่ไม่ถูกต้องใน Windows 8
BrutalDev

คำตอบจาก Simon Mourier คือคำตอบที่ดีที่สุดที่นี่ วิธีเดียวที่ถูกต้องและพิสูจน์สัญลักษณ์แสดงหัวข้อย่อยในการสร้างทางลัดคือใช้ API เดียวกับที่ระบบปฏิบัติการใช้และนี่คืออินเทอร์เฟซ IShellLink อย่าใช้ Windows Script Host หรือสร้างเว็บลิงค์! Simon Mourier แสดงวิธีการทำด้วยรหัส 6 บรรทัด ใครก็ตามที่มีปัญหาเกี่ยวกับวิธีนี้แน่นอนว่าผ่านเส้นทางที่ไม่ถูกต้อง ฉันทดสอบโค้ดของเขาใน Windows XP, 7 และ 10 รวบรวมแอปของคุณเป็น "CPU ใด ๆ " เพื่อหลีกเลี่ยงปัญหากับ Windows 32/64 บิตที่ใช้โฟลเดอร์ต่างกันสำหรับ Program Files และอื่น ๆ
Elmue

คำตอบ:


120

ด้วยตัวเลือกเพิ่มเติมเช่นปุ่มลัดคำอธิบายเป็นต้น

ในตอนแรก Project > Add Reference > COM > Windows Script Host Object Model

using IWshRuntimeLibrary;

private void CreateShortcut()
{
  object shDesktop = (object)"Desktop";
  WshShell shell = new WshShell();
  string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\Notepad.lnk";
  IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
  shortcut.Description = "New shortcut for a Notepad";
  shortcut.Hotkey = "Ctrl+Shift+N";
  shortcut.TargetPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\notepad.exe";
  shortcut.Save();
}

2
นี่เป็นเรื่องใกล้ตัวสำหรับฉันจริงๆ ฉันต้องการเพิ่มไดเรกทอรีของ. exe ในคุณสมบัติ "WorkingDirectory" บนทางลัด (
short.WorkingDirectory

4
ในการระบุดัชนีไอคอน (ใน IconLocation) ให้ใช้ค่าเช่น "path_to_icon_file, #" โดยที่ # คือดัชนีไอคอน ดูmsdn.microsoft.com/en-us/library/xsy6k3ys(v=vs.84).aspx
คริส

1
สำหรับอาร์กิวเมนต์: ทางลัด.Arguments = "Seta Map mp_crash"; stackoverflow.com/a/18491229/2155778
Zolfaghari

7
Environment.SpecialFolders.System - ไม่มีอยู่ ... Environment.SpecialFolder.System - ทำงาน
JSWulf

สำหรับเวลาที่คุณต้องเพิ่ม Microsoft.CSharp เป็นข้อมูลอ้างอิง
l1nuxuser

77

ทางลัด URL

private void urlShortcutToDesktop(string linkName, string linkUrl)
{
    string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

    using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
    {
        writer.WriteLine("[InternetShortcut]");
        writer.WriteLine("URL=" + linkUrl);
    }
}

ทางลัดแอปพลิเคชัน

private void appShortcutToDesktop(string linkName)
{
    string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

    using (StreamWriter writer = new StreamWriter(deskDir + "\\" + linkName + ".url"))
    {
        string app = System.Reflection.Assembly.GetExecutingAssembly().Location;
        writer.WriteLine("[InternetShortcut]");
        writer.WriteLine("URL=file:///" + app);
        writer.WriteLine("IconIndex=0");
        string icon = app.Replace('\\', '/');
        writer.WriteLine("IconFile=" + icon);
    }
}

ตรวจสอบตัวอย่างนี้ด้วย

หากคุณต้องการใช้ฟังก์ชันเฉพาะของ API คุณจะต้องใช้IShellLink interfaceเช่นเดียวกับIPersistFile interface(ผ่าน COM interop)

นี่คือบทความที่ให้รายละเอียดเกี่ยวกับสิ่งที่คุณต้องทำรวมถึงโค้ดตัวอย่าง


ข้างต้นทำงานได้ดี แต่ฉันต้องการสร้างทางลัดผ่านฟังก์ชัน API บางอย่างเช่น DllImport ("coredll.dll")] int SHCreateShortcut แบบคงที่สาธารณะ (StringBuilder szShortcut, StringBuilder szTarget);
Vipin Arora

@Vipin ทำไม? มีสาเหตุใดบ้างที่วิธีแก้ไขปัญหาข้างต้นไม่ดีพอ?
alex

8
nitpicking: คุณสามารถลบบรรทัด flush () ได้เนื่องจากการสิ้นสุดของ Using block ควรดูแลคุณ
Newtopian

3
ฉันมีปัญหามากมายกับวิธีนี้ ... Windows มีแนวโน้มที่จะแคชคำจำกัดความของช็อตคัทไว้ที่ไหนสักแห่ง ... สร้างช็อตคัทแบบนี้ลบออกจากนั้นสร้างด้วยชื่อเดียวกัน แต่เป็น URL ที่แตกต่างกัน ... จะเปิด URL เก่าที่ถูกลบเมื่อคุณคลิกทางลัด คำตอบของ Rustam ด้านล่าง (ใช้. lnk แทน. url) แก้ปัญหานี้ให้ฉัน
TCC

1
คำตอบที่ยอดเยี่ยม ดีกว่าท่อประปา COM ที่น่ากลัวที่คุณต้องจัดการเมื่อใช้ไฟล์. lnk
James Ko

61

นี่คือโค้ดส่วนหนึ่งที่ไม่มีการพึ่งพาอ็อบเจ็กต์ COM ภายนอก (WSH) และรองรับโปรแกรม 32 บิตและ 64 บิต:

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;

namespace TestShortcut
{
    class Program
    {
        static void Main(string[] args)
        {
            IShellLink link = (IShellLink)new ShellLink();

            // setup shortcut information
            link.SetDescription("My Description");
            link.SetPath(@"c:\MyPath\MyProgram.exe");

            // save it
            IPersistFile file = (IPersistFile)link;
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            file.Save(Path.Combine(desktopPath, "MyLink.lnk"), false);
        }
    }

    [ComImport]
    [Guid("00021401-0000-0000-C000-000000000046")]
    internal class ShellLink
    {
    }

    [ComImport]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("000214F9-0000-0000-C000-000000000046")]
    internal interface IShellLink
    {
        void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, int fFlags);
        void GetIDList(out IntPtr ppidl);
        void SetIDList(IntPtr pidl);
        void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
        void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
        void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
        void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
        void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
        void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
        void GetHotkey(out short pwHotkey);
        void SetHotkey(short wHotkey);
        void GetShowCmd(out int piShowCmd);
        void SetShowCmd(int iShowCmd);
        void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
        void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
        void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
        void Resolve(IntPtr hwnd, int fFlags);
        void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
    }
}

@BrutalDev - อะไรไม่ได้ผล? ฉันได้ทดสอบบน Windows 8 x64 และใช้งานได้
Simon Mourier

นอกจากนี้ยังเรียกใช้ Win8 x64 คัดลอกตัวอย่างโค้ดด้านบนตามที่เป็นอยู่มันสร้างไอคอนบนเดสก์ท็อปของฉันโดยไม่มีเส้นทาง การดำเนินการลิงก์เพียงแค่เปิด explorer ไปที่เดสก์ท็อป นี่เป็นปัญหาที่คล้ายกันกับ ShellLink.cs แต่ใน Windows XP / 2003 ตัวอย่างเดียวที่ใช้งานได้ชัดเจนใน Windows ทุกเวอร์ชันคือ Rustam Irzaev ใช้ WSHOM ตามที่ฉันกล่าวไว้ในความคิดเห็นของฉันสำหรับคำถามหลัก: "นี่เป็นสิ่งที่มีแนวโน้มมาก แต่สร้างทางลัดที่ไม่ถูกต้องใน Windows 8"
BrutalDev

ฉันทำให้สิ่งนี้ทำงานบน Windows 8.1 x64 แต่รหัสที่ให้ไว้ตอนนี้ไม่มีคำจำกัดความสำหรับ IPersistFile ฉันต้องคัดลอกสิ่งนั้นจากโพสต์ShellLink.csเพื่อให้ใช้งานได้
Walter Wilfinger

ฉันไม่เห็นเหตุผลที่จับต้องได้ว่าทำไมถึงใช้ไม่ได้ อย่างไรก็ตาม IPersistFile สามารถใช้งานได้ทันทีใน System.Runtime.InteropServices.ComTypes
Simon Mourier

1
โซลูชันนี้ไม่ได้ตั้งค่าไอคอนที่ถูกต้องโดยใช้SetIconLocationบน Windows 10 64 บิตพร้อมปฏิบัติการ 32 บิต มีการอธิบายวิธีแก้ปัญหาไว้ที่นี่: stackoverflow.com/a/39282861และฉันยังสงสัยว่าเป็นปัญหาเดียวกันกับ Windows 8 อื่น ๆ ทั้งหมดที่เกี่ยวข้อง อาจเกี่ยวข้องกับไฟล์ exe 32 บิตบน Windows 64 บิต
Maris B.

26

คุณสามารถใช้คลาสShellLink.csนี้เพื่อสร้างทางลัด

ในการรับไดเร็กทอรีเดสก์ท็อปให้ใช้:

var dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

หรือใช้Environment.SpecialFolder.CommonDesktopDirectoryเพื่อสร้างสำหรับผู้ใช้ทุกคน


6
@Vipin: หากวิธีแก้ปัญหาเหมาะกับคุณเป็นเรื่องปกติที่จะต้องโหวตให้คะแนน นอกจากนี้คุณควรเลือกวิธีแก้ปัญหาที่ดีที่สุดและยอมรับว่าเป็นคำตอบสำหรับปัญหาของคุณ
Thorarin

สิ่งนี้จะเขียนทับ exe ที่มีอยู่ด้วยไฟล์ lnk ทดสอบบน Win10
zwcloud

@zwcloud รหัสนี้ไม่ได้เขียนทับอะไรเพราะไม่ได้ทำอะไรเลย เป็นเพียงการบอกคุณว่าจะใช้คลาสและวิธีการใดในการทำงานกับทางลัด หากรหัสของคุณเขียนทับ exe ที่เป็นของคุณ ฉันจะดูว่าคุณสร้างไฟล์ lnk อย่างไรเพื่อดูว่าทำไมมันถึงทำลาย exe ของคุณ
Cdaragorn

15

โดยไม่ต้องอ้างอิงเพิ่มเติม:

using System;
using System.Runtime.InteropServices;

public class Shortcut
{

private static Type m_type = Type.GetTypeFromProgID("WScript.Shell");
private static object m_shell = Activator.CreateInstance(m_type);

[ComImport, TypeLibType((short)0x1040), Guid("F935DC23-1CF0-11D0-ADB9-00C04FD58A0B")]
private interface IWshShortcut
{
    [DispId(0)]
    string FullName { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0)] get; }
    [DispId(0x3e8)]
    string Arguments { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e8)] set; }
    [DispId(0x3e9)]
    string Description { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3e9)] set; }
    [DispId(0x3ea)]
    string Hotkey { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ea)] set; }
    [DispId(0x3eb)]
    string IconLocation { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3eb)] set; }
    [DispId(0x3ec)]
    string RelativePath { [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ec)] set; }
    [DispId(0x3ed)]
    string TargetPath { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ed)] set; }
    [DispId(0x3ee)]
    int WindowStyle { [DispId(0x3ee)] get; [param: In] [DispId(0x3ee)] set; }
    [DispId(0x3ef)]
    string WorkingDirectory { [return: MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] get; [param: In, MarshalAs(UnmanagedType.BStr)] [DispId(0x3ef)] set; }
    [TypeLibFunc((short)0x40), DispId(0x7d0)]
    void Load([In, MarshalAs(UnmanagedType.BStr)] string PathLink);
    [DispId(0x7d1)]
    void Save();
}

public static void Create(string fileName, string targetPath, string arguments, string workingDirectory, string description, string hotkey, string iconPath)
{
    IWshShortcut shortcut = (IWshShortcut)m_type.InvokeMember("CreateShortcut", System.Reflection.BindingFlags.InvokeMethod, null, m_shell, new object[] { fileName });
    shortcut.Description = description;
    shortcut.Hotkey = hotkey;
    shortcut.TargetPath = targetPath;
    shortcut.WorkingDirectory = workingDirectory;
    shortcut.Arguments = arguments;
    if (!string.IsNullOrEmpty(iconPath))
        shortcut.IconLocation = iconPath;
    shortcut.Save();
}
}

ในการสร้างทางลัดบนเดสก์ท็อป:

    string lnkFileName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Notepad.lnk");
    Shortcut.Create(lnkFileName,
        System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "notepad.exe"),
        null, null, "Open Notepad", "Ctrl+Shift+N", null);

11

ฉันใช้สำหรับแอพของฉัน:

using IWshRuntimeLibrary; // > Ref > COM > Windows Script Host Object  
...   
private static void CreateShortcut()
    {
        string link = Environment.GetFolderPath( Environment.SpecialFolder.Desktop ) 
            + Path.DirectorySeparatorChar + Application.ProductName + ".lnk";
        var shell = new WshShell();
        var shortcut = shell.CreateShortcut( link ) as IWshShortcut;
        shortcut.TargetPath = Application.ExecutablePath;
        shortcut.WorkingDirectory = Application.StartupPath;
        //shortcut...
        shortcut.Save();
    }

ทำงานนอกกรอบเพียงแค่คัดลอกวาง
rluks

9

ใช้ShellLink.csที่ vbAccelerator เพื่อสร้างทางลัดของคุณได้อย่างง่ายดาย!

private static void AddShortCut()
{
using (ShellLink shortcut = new ShellLink())
{
    shortcut.Target = Application.ExecutablePath;
    shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
    shortcut.Description = "My Shorcut";
    shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
    shortcut.Save(SHORTCUT_FILEPATH);
}
}

3
การเชื่อมโยงที่ตอนนี้ตาย แต่คุณสามารถหารุ่นที่เก็บไว้มันนี่
pswg

7

นี่คือรหัสของฉัน:

public static class ShortcutHelper
{
    #region Constants
    /// <summary>
    /// Default shortcut extension
    /// </summary>
    public const string DEFAULT_SHORTCUT_EXTENSION = ".lnk";

    private const string WSCRIPT_SHELL_NAME = "WScript.Shell";
    #endregion

    /// <summary>
    /// Create shortcut in current path.
    /// </summary>
    /// <param name="linkFileName">shortcut name(include .lnk extension.)</param>
    /// <param name="targetPath">target path</param>
    /// <param name="workingDirectory">working path</param>
    /// <param name="arguments">arguments</param>
    /// <param name="hotkey">hot key(ex: Ctrl+Shift+Alt+A)</param>
    /// <param name="shortcutWindowStyle">window style</param>
    /// <param name="description">shortcut description</param>
    /// <param name="iconNumber">icon index(start of 0)</param>
    /// <returns>shortcut file path.</returns>
    /// <exception cref="System.IO.FileNotFoundException"></exception>
    public static string CreateShortcut(
        string linkFileName,
        string targetPath,
        string workingDirectory = "",
        string arguments = "",
        string hotkey = "",
        ShortcutWindowStyles shortcutWindowStyle = ShortcutWindowStyles.WshNormalFocus,
        string description = "",
        int iconNumber = 0)
    {
        if (linkFileName.Contains(DEFAULT_SHORTCUT_EXTENSION) == false)
        {
            linkFileName = string.Format("{0}{1}", linkFileName, DEFAULT_SHORTCUT_EXTENSION);
        }

        if (File.Exists(targetPath) == false)
        {
            throw new FileNotFoundException(targetPath);
        }

        if (workingDirectory == string.Empty)
        {
            workingDirectory = Path.GetDirectoryName(targetPath);
        }

        string iconLocation = string.Format("{0},{1}", targetPath, iconNumber);

        if (Environment.Version.Major >= 4)
        {
            Type shellType = Type.GetTypeFromProgID(WSCRIPT_SHELL_NAME);
            dynamic shell = Activator.CreateInstance(shellType);
            dynamic shortcut = shell.CreateShortcut(linkFileName);

            shortcut.TargetPath = targetPath;
            shortcut.WorkingDirectory = workingDirectory;
            shortcut.Arguments = arguments;
            shortcut.Hotkey = hotkey;
            shortcut.WindowStyle = shortcutWindowStyle;
            shortcut.Description = description;
            shortcut.IconLocation = iconLocation;

            shortcut.Save();
        }
        else
        {
            Type shellType = Type.GetTypeFromProgID(WSCRIPT_SHELL_NAME);
            object shell = Activator.CreateInstance(shellType);
            object shortcut = shellType.InvokeMethod("CreateShortcut", shell, linkFileName);
            Type shortcutType = shortcut.GetType();

            shortcutType.InvokeSetMember("TargetPath", shortcut, targetPath);
            shortcutType.InvokeSetMember("WorkingDirectory", shortcut, workingDirectory);
            shortcutType.InvokeSetMember("Arguments", shortcut, arguments);
            shortcutType.InvokeSetMember("Hotkey", shortcut, hotkey);
            shortcutType.InvokeSetMember("WindowStyle", shortcut, shortcutWindowStyle);
            shortcutType.InvokeSetMember("Description", shortcut, description);
            shortcutType.InvokeSetMember("IconLocation", shortcut, iconLocation);

            shortcutType.InvokeMethod("Save", shortcut);
        }

        return Path.Combine(System.Windows.Forms.Application.StartupPath, linkFileName);
    }

    private static object InvokeSetMember(this Type type, string methodName, object targetInstance, params object[] arguments)
    {
        return type.InvokeMember(
            methodName,
            BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
            null,
            targetInstance,
            arguments);
    }

    private static object InvokeMethod(this Type type, string methodName, object targetInstance, params object[] arguments)
    {
        return type.InvokeMember(
            methodName,
            BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
            null,
            targetInstance,
            arguments);
    }

    /// <summary>
    /// windows styles
    /// </summary>
    public enum ShortcutWindowStyles
    {
        /// <summary>
        /// Hide
        /// </summary>
        WshHide = 0,
        /// <summary>
        /// NormalFocus
        /// </summary>
        WshNormalFocus = 1,
        /// <summary>
        /// MinimizedFocus
        /// </summary>
        WshMinimizedFocus = 2,
        /// <summary>
        /// MaximizedFocus
        /// </summary>
        WshMaximizedFocus = 3,
        /// <summary>
        /// NormalNoFocus
        /// </summary>
        WshNormalNoFocus = 4,
        /// <summary>
        /// MinimizedNoFocus
        /// </summary>
        WshMinimizedNoFocus = 6,
    }
}

5

แก้ไข:ฉันไม่แนะนำวิธีแก้ปัญหานี้อีกต่อไป หากยังไม่มีวิธีใดที่ดีไปกว่าการใช้ Windows Scripting Engine อย่างน้อยก็ให้ใช้โซลูชันของ @ Mehmet ซึ่งเรียกใช้เอ็นจิ้นโดยตรงแทนที่จะสร้างสคริปต์ข้อความธรรมดาในหน่วยความจำ

เราใช้ VBScript เพื่อสร้างทางลัด ไม่จำเป็นต้องใช้ p / Invoke, COM Interop และ DLL เพิ่มเติม การทำงานเช่นนี้:

  • สร้าง VBScript ที่รันไทม์ด้วยพารามิเตอร์ที่ระบุของเมธอด CreateShortcut C #
  • บันทึก VBScript นี้ในไฟล์ชั่วคราว
  • รอให้สคริปต์เสร็จสิ้น
  • ลบไฟล์ชั่วคราว

ได้แล้ว:

static string _scriptTempFilename;

/// <summary>
/// Creates a shortcut at the specified path with the given target and
/// arguments.
/// </summary>
/// <param name="path">The path where the shortcut will be created. This should
///     be a file with the LNK extension.</param>
/// <param name="target">The target of the shortcut, e.g. the program or file
///     or folder which will be opened.</param>
/// <param name="arguments">The additional command line arguments passed to the
///     target.</param>
public static void CreateShortcut(string path, string target, string arguments)
{
    // Check if link path ends with LNK or URL
    string extension = Path.GetExtension(path).ToUpper();
    if (extension != ".LNK" && extension != ".URL")
    {
        throw new ArgumentException("The path of the shortcut must have the extension .lnk or .url.");
    }

    // Get temporary file name with correct extension
    _scriptTempFilename = Path.GetTempFileName();
    File.Move(_scriptTempFilename, _scriptTempFilename += ".vbs");

    // Generate script and write it in the temporary file
    File.WriteAllText(_scriptTempFilename, String.Format(@"Dim WSHShell
Set WSHShell = WScript.CreateObject({0}WScript.Shell{0})
Dim Shortcut
Set Shortcut = WSHShell.CreateShortcut({0}{1}{0})
Shortcut.TargetPath = {0}{2}{0}
Shortcut.WorkingDirectory = {0}{3}{0}
Shortcut.Arguments = {0}{4}{0}
Shortcut.Save",
        "\"", path, target, Path.GetDirectoryName(target), arguments),
        Encoding.Unicode);

    // Run the script and delete it after it has finished
    Process process = new Process();
    process.StartInfo.FileName = _scriptTempFilename;
    process.Start();
    process.WaitForExit();
    File.Delete(_scriptTempFilename);
}

3

นี่คือวิธีการขยาย (ทดสอบแล้ว) พร้อมความคิดเห็นที่จะช่วยคุณได้

using IWshRuntimeLibrary;
using System;

namespace Extensions
{
    public static class XShortCut
    {
        /// <summary>
        /// Creates a shortcut in the startup folder from a exe as found in the current directory.
        /// </summary>
        /// <param name="exeName">The exe name e.g. test.exe as found in the current directory</param>
        /// <param name="startIn">The shortcut's "Start In" folder</param>
        /// <param name="description">The shortcut's description</param>
        /// <returns>The folder path where created</returns>
        public static string CreateShortCutInStartUpFolder(string exeName, string startIn, string description)
        {
            var startupFolderPath = Environment.SpecialFolder.Startup.GetFolderPath();
            var linkPath = startupFolderPath + @"\" + exeName + "-Shortcut.lnk";
            var targetPath = Environment.CurrentDirectory + @"\" + exeName;
            XFile.Delete(linkPath);
            Create(linkPath, targetPath, startIn, description);
            return startupFolderPath;
        }

        /// <summary>
        /// Create a shortcut
        /// </summary>
        /// <param name="fullPathToLink">the full path to the shortcut to be created</param>
        /// <param name="fullPathToTargetExe">the full path to the exe to 'really execute'</param>
        /// <param name="startIn">Start in this folder</param>
        /// <param name="description">Description for the link</param>
        public static void Create(string fullPathToLink, string fullPathToTargetExe, string startIn, string description)
        {
            var shell = new WshShell();
            var link = (IWshShortcut)shell.CreateShortcut(fullPathToLink);
            link.IconLocation = fullPathToTargetExe;
            link.TargetPath = fullPathToTargetExe;
            link.Description = description;
            link.WorkingDirectory = startIn;
            link.Save();
        }
    }
}

และตัวอย่างการใช้งาน:

XShortCut.CreateShortCutInStartUpFolder(THEEXENAME, 
    Environment.CurrentDirectory,
    "Starts some executable in the current directory of application");

พาร์มที่ 1 ตั้งค่าชื่อ exe (พบในไดเร็กทอรีปัจจุบัน) พาร์มที่ 2 คือโฟลเดอร์ "เริ่มใน" และพาร์มที่ 3 คือคำอธิบายทางลัด

ตัวอย่างการใช้รหัสนี้

รูปแบบการตั้งชื่อของลิงก์ไม่ทำให้เกิดความคลุมเครือว่าจะทำอะไร หากต้องการทดสอบลิงก์เพียงดับเบิลคลิก

หมายเหตุสุดท้าย: แอปพลิเคชันเอง (เป้าหมาย) ต้องมีภาพไอคอนที่เกี่ยวข้อง ลิงค์นี้สามารถค้นหาไอคอนภายใน exe ได้อย่างง่ายดาย หากแอปพลิเคชันเป้าหมายมีไอคอนมากกว่าหนึ่งไอคอนคุณสามารถเปิดคุณสมบัติของลิงก์และเปลี่ยนไอคอนเป็นไอคอนอื่นที่พบใน exe


ฉันได้รับข้อความแสดงข้อผิดพลาดที่ไม่มี. GetFolderPath () เช่นเดียวกับ XFile.Delete ฉันขาดอะไรไป?
RalphF

เกิดข้อผิดพลาดที่นี่หรือไม่ Environment.SpecialFolder.Startup.GetFolderPath ();
John Peters

2

ฉันใช้การอ้างอิง "Windows Script Host Object Model" เพื่อสร้างทางลัด

การเพิ่ม "Windows Script Host Object Model" ในการอ้างอิงโครงการ

และเพื่อสร้างทางลัดในสถานที่เฉพาะ:

    void CreateShortcut(string linkPath, string filename)
    {
        // Create shortcut dir if not exists
        if (!Directory.Exists(linkPath))
            Directory.CreateDirectory(linkPath);

        // shortcut file name
        string linkName = Path.ChangeExtension(Path.GetFileName(filename), ".lnk");

        // COM object instance/props
        IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
        IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(linkName);
        sc.Description = "some desc";
        //shortcut.IconLocation = @"C:\..."; 
        sc.TargetPath = linkPath;
        // save shortcut to target
        sc.Save();
    }

0
private void CreateShortcut(string executablePath, string name)
    {
        CMDexec("echo Set oWS = WScript.CreateObject('WScript.Shell') > CreateShortcut.vbs");
        CMDexec("echo sLinkFile = '" + Environment.GetEnvironmentVariable("homedrive") + "\\users\\" + Environment.GetEnvironmentVariable("username") + "\\desktop\\" + name + ".ink' >> CreateShortcut.vbs");
        CMDexec("echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs");
        CMDexec("echo oLink.TargetPath = '" + executablePath + "' >> CreateShortcut.vbs");
        CMDexec("echo oLink.Save >> CreateShortcut.vbs");
        CMDexec("cscript CreateShortcut.vbs");
        CMDexec("del CreateShortcut.vbs");
    }

0

ฉันได้สร้างคลาส wrapper ตามคำตอบของ Rustam Irzaev โดยใช้ IWshRuntimeLibrary

IWshRuntimeLibrary -> การอ้างอิง -> COM> Windows Script Host Object Model

using System;
using System.IO;
using IWshRuntimeLibrary;
using File = System.IO.File;

public static class Shortcut
{
    public static void CreateShortcut(string originalFilePathAndName, string destinationSavePath)
    {
        string fileName = Path.GetFileNameWithoutExtension(originalFilePathAndName);
        string originalFilePath = Path.GetDirectoryName(originalFilePathAndName);

        string link = destinationSavePath + Path.DirectorySeparatorChar + fileName + ".lnk";
        var shell = new WshShell();
        var shortcut = shell.CreateShortcut(link) as IWshShortcut;
        if (shortcut != null)
        {
            shortcut.TargetPath = originalFilePathAndName;
            shortcut.WorkingDirectory = originalFilePath;
            shortcut.Save();
        }
    }

    public static void CreateStartupShortcut()
    {
        CreateShortcut(System.Reflection.Assembly.GetEntryAssembly()?.Location, Environment.GetFolderPath(Environment.SpecialFolder.Startup));
    }

    public static void DeleteShortcut(string originalFilePathAndName, string destinationSavePath)
    {
        string fileName = Path.GetFileNameWithoutExtension(originalFilePathAndName);
        string originalFilePath = Path.GetDirectoryName(originalFilePathAndName);

        string link = destinationSavePath + Path.DirectorySeparatorChar + fileName + ".lnk";
        if (File.Exists(link)) File.Delete(link);
    }

    public static void DeleteStartupShortcut()
    {
        DeleteShortcut(System.Reflection.Assembly.GetEntryAssembly()?.Location, Environment.GetFolderPath(Environment.SpecialFolder.Startup));
    }
}

-2

สำหรับ Windows Vista / 7/8/10 คุณสามารถสร้าง symlink แทนผ่านทางmklink.

Process.Start("cmd.exe", $"/c mklink {linkName} {applicationPath}");

หรือโทรCreateSymbolicLinkผ่าน P / Invoke


สิ่งนี้ไม่เกี่ยวข้องกับทางลัด
Matt
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.