C#用控制台应用程序创建快捷方式 [英] C# creating a shortcut with console application
本文介绍了C#用控制台应用程序创建快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的C#控制台应用程序创建自己的快捷方式?
我不知道从哪里开始
是这可能吗?
我尝试了什么:
我不喜欢知道从哪里开始,因为我是c#的新手并且仍在学习
I want my C# console application to create a shortcut of its self?
I don't know where to begin
Is this possible?
What I have tried:
I don't know where to begin as I am new to c# and still learning
推荐答案
试试这个:
Try this:
[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);
}
private void CreateLink(string pathToApplication, string description, string linkname = "MyApplication.lnk")
{
IShellLink shortcut = (IShellLink)new ShellLink();
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
shortcut.SetDescription(description);
shortcut.SetPath(pathToApplication);
System.Runtime.InteropServices.ComTypes.IPersistFile f = (System.Runtime.InteropServices.ComTypes.IPersistFile) shortcut;
f.Save(Path.Combine(desktop, linkname), false);
}
我讨厌加价![/ edit]
[edit]I hate markup![/edit]
感谢OriginalGriff。
它工作得很漂亮。
只是增强了CreateLink方法以获得更好的可用性。
Thanks OriginalGriff.
It works beautifully.
Just enhanced the CreateLink method for better usability.
public static void CreateLink(String srcPath, String shortcutPath,
String description = null)
{
// Derive params
if (description == null)
description = "Opens " + Path.GetFileNameWithoutExtension(srcPath);
if (!shortcutPath.EndsWith(".lnk", StringComparison.OrdinalIgnoreCase))
shortcutPath += ".lnk";
// Initialize Shortcut
var link = (IShellLink)new ShellLink();
link.SetDescription(description);
link.SetPath(srcPath);
link.SetWorkingDirectory(Path.GetDirectoryName(srcPath));
// Save
var pf = (IPersistFile)link;
pf.Save(shortcutPath, false);
}
这篇关于C#用控制台应用程序创建快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文