在c#中创建一个文件夹的快捷方式#
为了长话短说,我需要使用C#创建一个文件夹的快捷方式。我一直在阅读使用IWshRuntimeLibrary
。当我去尝试使用IWshRuntimeLibrary
时,我得到各种模糊错误System.IO.File
。我假设这是因为有一个IWshRuntimeLibrary.File
接口以及System.IO.File
。我真正能够找到的是关于制作应用程序快捷方式的文章,而不是文件夹。在c#中创建一个文件夹的快捷方式#
歧义错误旁白:
- 这是使用文件夹快捷方式正确的工具?
- 我如何使用这个
- 如何指定,当我尝试创建一个快捷方式文件夹,快捷方式放置
另外创建一个快捷方式,说C:\TEMP
使用此:
IWshShortcut shortcut;
wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(@"C:\TEMP");
shortcut.TargetPath = @"C:\Documents and Settings";
我得到COMException
。根据我读过的内容,应该创建一个到C盘temp文件夹的快捷方式,并将快捷方式放在Documents and Settings中。
不要忘记将Embed Interop Types设置为False来引用Interop.IWshRuntimeLibrary。 我测试过并没有收到错误。
// Make sure you use try/catch block because your App may has no permissions on the target path!
try
{
CreateShortcut(@"C:\temp", @"C:\MyShortcutFile.lnk",
"Custom Shortcut", "/param", "Ctrl+F", @"c:\");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
/// <summary>
/// Create Windows Shorcut
/// </summary>
/// <param name="SourceFile">A file you want to make shortcut to</param>
/// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
public static void CreateShortcut(string SourceFile, string ShortcutFile)
{
CreateShortcut(SourceFile, ShortcutFile, null, null, null, null);
}
/// <summary>
/// Create Windows Shorcut
/// </summary>
/// <param name="SourceFile">A file you want to make shortcut to</param>
/// <param name="ShortcutFile">Path and shorcut file name including file extension (.lnk)</param>
/// <param name="Description">Shortcut description</param>
/// <param name="Arguments">Command line arguments</param>
/// <param name="HotKey">Shortcut hot key as a string, for example "Ctrl+F"</param>
/// <param name="WorkingDirectory">"Start in" shorcut parameter</param>
public static void CreateShortcut(string TargetPath, string ShortcutFile, string Description,
string Arguments, string HotKey, string WorkingDirectory)
{
// Check necessary parameters first:
if (String.IsNullOrEmpty(TargetPath))
throw new ArgumentNullException("TargetPath");
if (String.IsNullOrEmpty(ShortcutFile))
throw new ArgumentNullException("ShortcutFile");
// Create WshShellClass instance:
var wshShell = new WshShellClass();
// Create shortcut object:
IWshRuntimeLibrary.IWshShortcut shorcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(ShortcutFile);
// Assign shortcut properties:
shorcut.TargetPath = TargetPath;
shorcut.Description = Description;
if (!String.IsNullOrEmpty(Arguments))
shorcut.Arguments = Arguments;
if (!String.IsNullOrEmpty(HotKey))
shorcut.Hotkey = HotKey;
if (!String.IsNullOrEmpty(WorkingDirectory))
shorcut.WorkingDirectory = WorkingDirectory;
// Save the shortcut:
shorcut.Save();
}
来源:http://zayko.net/post/How-to-create-Windows-shortcut-(C).aspx
正是我在找的东西,我也接近这个解决方案。只是没有正确命名我的.lnk快捷方式文件 – swabs
是专家交换使用IShellLink
A液:
提供了托管层为您打造文件和文件夹shorcuts。
它是VB.NET,但您可以使用免费转换器将其转换为C#。
你得到它周围的其他方法:你想使C:\Temp
快速进入C:\Documents and Settings
文件夹。
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
IWshShortcut shortcut;
var wshShell = new WshShellClass();
shortcut = (IWshShortcut)wshShell.CreateShortcut(Path.Combine(desktop, @"Temp.lnk"));
shortcut.TargetPath = @"\\computername\sharename";
shortcut.Save();
我意识到这是永远的,但我试图用这种方法来创建一个名为类似2.7.1的文件夹的链接,当我尝试点击由此产生的快捷方式,Windows会尝试像打开文件一样打开文件夹。有关如何确保Windows知道这是文件夹的快捷方式的任何想法? –
可能重复:http://*.com/questions/3391923/placing-a-shortcut-in-users-
下面的代码片段将在桌面上创建一个快捷方式到网络文件夹启动文件夹以启动Windows – 2012-10-22 20:33:11
为了摆脱歧义错误,请确保何时使用.NET File类,调用System.IO.File,并在使用其他类时使用IWShRuntimeLibrary.File 。如果我想在使用System.Windows.Forms和System.Threading的类中使用Timer,因为它们都包含Timer类,所以我一直都会遇到这个问题。抱歉,我无法帮助您解决主要问题。 –
@ademing2,我有点想到这是我必须做的,但在添加之前,我的所有System.IO.File方法调用之前,我想看看我是否在正确的轨道上。虽然 – swabs