隐藏

C# 创建网站快捷方式 .Lnk

发布:2020/7/7 10:43:57作者:管理员 来源:本站 浏览次数:1041

有个需求。需要把新的网站给客户机装上快捷方式,发现显示的图标在不同系统上显示虚。或者小(很大的空白)。

查看了网站快捷方式的源代码(用记事本打开)一个.url文件

[InternetShortcut]
URL=http://www.nsoff.com
IconIndex=0
IconFile=http://www.nsoff.com/fav.ico

 

类拟这种文件 而这个ICO文件在桌面显示时要么小。要么虚。,

之后我又传一个大的ICO上去。把地址相应的改了。就不显示图标了。

 

后想到lnk文件。

lnk文件是用于指向其他文件的一种文件。 这些文件通常称为快捷方式文件.通常它以快捷方式放在硬盘上.以方便使用者快速的调用
其扩展名为.lnk

用代码自动创建lnk文件。并指定相应的图标和URl。

以下是C#代码

注:需要引用com组件  Windows Script Host Object Model


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;//互动服务
using IWshRuntimeLibrary;
using System.IO;

namespace phpmysql
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("准备生成");
            //CreateDesktopShortcut("", "phpmysql", "phpmysql");
            //CreateUrl("phpmysql","http://www.begon.cn", "phpmysql");
            //CreateDesktop();
            //CreateShortcut("IT鸟的专栏-博客圆", "http://www.cnblogs.com/ITniao/");
            CreateWebsite("柏港","http://www.begon.cn");
            Console.WriteLine("成功生成");
            Console.ReadKey();
        }

        static void CreateUrl(string name,string url,string icoPath) {
            string deskDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//桌面路径
            using (StreamWriter writer = new StreamWriter(deskDir + "\\" + name + ".url"))
            {
                writer.WriteLine("[InternetShortcut]");
                writer.WriteLine("URL=" + url);//网址也可以是ftp地址
                writer.WriteLine("ShowCommand=3");//浏览器打开的方式,0或者不设置是正常,3是最大,7是最小
                writer.WriteLine("IconIndex=0");//ico的顺序,默认设置为0就可以
                writer.WriteLine("IconFile=" + icoPath);//ICO路径,可以是dll、exe、ico等格式
                writer.Flush();

            }


        }

        /// <summary>
        /// 创建桌面快捷方式
        /// </summary>
        /// <param name="deskTop">桌面的路径</param>
        /// <param name="FileName">文件的名称</param>
        /// <param name="exePath">EXE的路径</param>
        /// <returns>成功或失败</returns>
        static bool CreateDesktopShortcut(string deskTop, string FileName, string exePath)
        {
            try
            {
                deskTop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
                if (System.IO.File.Exists(deskTop + FileName + ".lnk"))  //
                {
                    System.IO.File.Delete(deskTop + FileName + ".lnk");//删除原来的桌面快捷键方式
                }
                WshShell shell = new WshShell();

                //快捷键方式创建的位置、名称
                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(deskTop + FileName + ".lnk");
                shortcut.TargetPath = exePath; //目标文件
                                               //该属性指定应用程序的工作目录,当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
                shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
                shortcut.WindowStyle = 1; //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】
                shortcut.Description = FileName; //描述
                shortcut.IconLocation = exePath + "\\logo.ico";  //快捷方式图标
                shortcut.Arguments = "";
                //shortcut.Hotkey = "CTRL+ALT+F11"; // 快捷键
                shortcut.Save(); //必须调用保存快捷才成创建成功
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        static bool CreateDesktop() {
            string app = "http://www.begon.cn";
            string location1 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites) + "\\邪恶的小嘴的空间.url";
            string location2 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory) + "\\邪恶的小嘴的空间.url";
            string location3 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Programs) + "\\邪恶的小嘴的空间.url";

            try
            {
                // Create a Windows Script Host Shell class
                WshShell shell = new WshShell();
                // Define the shortcut file
                IWshURLShortcut shortcut = shell.CreateShortcut(location1) as IWshURLShortcut;
                shortcut.TargetPath = app;
                // Save it
                shortcut.Save();

                shortcut = shell.CreateShortcut(location2) as IWshURLShortcut;
                shortcut.TargetPath = app;
                // Save it
                shortcut.Save();

                shortcut = shell.CreateShortcut(location3) as IWshURLShortcut;
                shortcut.TargetPath = app;
                // Save it
                shortcut.Save();

                return true;
            }

            catch (COMException ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 创建快捷方式
        /// </summary>
        /// <param name="Title">标题</param>
        /// <param name="URL">URL地址</param>
        static void CreateShortcut(string Title, string URL)
        {
            string strFavoriteFolder;

            // “收藏夹”中 创建 IE 快捷方式
            //strFavoriteFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
            //CreateShortcutFile(Title, URL, strFavoriteFolder);

            // “ 桌面 ”中 创建 IE 快捷方式
            strFavoriteFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            CreateShortcutFile(Title, URL, strFavoriteFolder);

            // “ 链接 ”中 创建 IE 快捷方式
            //strFavoriteFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites) + "\\链接";
            //CreateShortcutFile(Title, URL, strFavoriteFolder);

            //「开始」菜单中 创建 IE 快捷方式
            //strFavoriteFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
            //CreateShortcutFile(Title, URL, strFavoriteFolder);

        }

        /// <summary>
        /// 创建快捷方式
        /// </summary>
        /// <param name="Title">标题</param>
        /// <param name="URL">URL地址</param>
        /// <param name="SpecialFolder">特殊文件夹</param>
        static void CreateShortcutFile(string Title, string URL, string SpecialFolder)
        {
            // Create shortcut file, based on Title
            System.IO.StreamWriter objWriter = System.IO.File.CreateText(SpecialFolder + "\\" + Title + ".url");
            // Write URL to file
            objWriter.WriteLine("[InternetShortcut]");
            objWriter.WriteLine("URL=" + URL);
            // Close file
            objWriter.Close();
        }
        /// <summary>
        /// C# 创建网站快捷方式 .Lnk
        /// </summary>
        /// <param name="name">网站名称</param>
        /// <param name="url">网址</param>
        static void CreateWebsite(string name,string url) {
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(
                Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                "\\" + name+".lnk"
                );
            shortcut.TargetPath = url;
            shortcut.Hotkey = "CTRL+F1";
            shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
            shortcut.WindowStyle = 1;
            shortcut.Description = "柏港科技";
            //shortcut.IconLocation = AppDomain.CurrentDomain.BaseDirectory.ToString() + "CreateUrl.exe,0";
            shortcut.IconLocation = "http://www.begon.cn/favicon.ico";
            shortcut.Save();
        }
    }
}


还是lnk文件好。还可以指定快捷键。直接打开了指定的网站。

主要说一下图标。

IconLocation属性。要么你给个全路径的图标地址。要么是一个资源的地址。

这里我把本身生成lnk程序的图标给指定成要生成的lnk的图标。

就可以通过 IconLocation给值为”可执行文件路径,0″的方式.

让lnk拥有我们指定的图标。

.lnk效果比.url好多了。。。