• C#自动创建快捷方式


    1.NET4.0之后动态类型版本

    public static void CreateShortcut(string lnkName)
    {
        var startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
        var lnkFilePath = Path.Combine(startup, $"{lnkName}.lnk");
        if (File.Exists(lnkFilePath ))
            return;
    
        var shellType = Type.GetTypeFromProgID("WScript.Shell");
        dynamic shell = Activator.CreateInstance(shellType);
        var shortcut = shell.CreateShortcut(lnkFilePath);
        shortcut.TargetPath = Assembly.GetEntryAssembly().Location;
        shortcut.Arguments = "";
        shortcut.WorkingDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        shortcut.Save();
    }
    

    2.NET3.5及之前反射版本

    public static void CreateShortcut(string lnkName)
    {
        var startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
        var lnkFilePath = Path.Combine(startup, $"{lnkName}.lnk");
        if (File.Exists(lnkFilePath ))
            return;
    
        var shellType = Type.GetTypeFromProgID("WScript.Shell");
        var shell = Activator.CreateInstance(shellType);
        var shortcut = shellType.InvokeMember("CreateShortcut", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, shell, new object[] { lnkFilePath });
        var shortcutType = shortcut.GetType();
        shortcutType.InvokeMember("TargetPath", BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null, shortcut, new object[] { Assembly.GetEntryAssembly().Location });
        shortcutType.InvokeMember("Arguments", BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null, shortcut, new object[] { args });
        shortcutType.InvokeMember("WorkingDirectory", BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty, null, shortcut, new object[] { AppDomain.CurrentDomain.SetupInformation.ApplicationBase });
        shortcutType.InvokeMember("Save", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, shortcut, null);
    }
    

    3.创建网页快捷方式

    public static void CreateUrlShortcut(string lnkFileName, string url, string iconFile)
    {
        if (File.Exists(lnkFileName))
            return;
    
        using (var writer = new StreamWriter(lnkFileName))
        {
            writer.WriteLine("[InternetShortcut]");
            writer.WriteLine("URL=" + url);
            writer.WriteLine("HotKey=0");
            writer.WriteLine("IconIndex=0");
            writer.WriteLine("IconFile=" + iconFile);
            writer.Flush();
        }
    }
    
  • 相关阅读:
    SpringBoot 创建 console程序
    SpringBoot 参数检查 Controller中检查参数是否合法
    SpringBoot 使用maven创建springboot项目
    idea 社区版本创建javaweb项目 使用jetty
    idea 社区版本创建javaweb项目 使用tomcat
    mysql 主从 设置
    windows 使用nginx
    Linux 安装Nginx
    闭包小应用
    js小程序写法优化
  • 原文地址:https://www.cnblogs.com/known/p/15390489.html
Copyright © 2020-2023  润新知