实现效果:
知识运用:
COM组件Windows Script Host Object Model
注意:拓展名为lnk 非 Ink //两个是不同的 写成大写的就明白了 :LNK INK
小写几乎没有区别 (为此每次运行都会报错 查了一大堆资料都没解决 注意!注意!)
实现代码:
private void button2_Click(object sender, EventArgs e) { if (textBox1.Text.Length == 0) { MessageBox.Show("请选择应用程序"); } else { string dtpath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\short.lnk"; string dtpath2 = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\自定义\short2.lnk"; string bp = Directory.GetParent(dtpath2).FullName; WshShell ws = new WshShell(); //创建WshShell对象 if (!Directory.Exists(bp)) //目录不存在 Directory.CreateDirectory(bp); IWshShortcut iw = (IWshShortcut)ws.CreateShortcut(dtpath); iw.TargetPath = textBox1.Text; //目的指向 iw.Description = "这是描述说明的内容"; iw.IconLocation = textBox1.Text; //加载图标的位置 iw.Save(); IWshShortcut iw2 = (IWshShortcut)ws.CreateShortcut(dtpath2); iw2.TargetPath = textBox1.Text; iw2.Description = textBox1.Text; iw2.IconLocation = textBox1.Text; iw2.Save(); MessageBox.Show("指定图标已经创建成功!"); } }