我试图在我的Windows文件系统上创建一个快捷方式(.lnk)。 我有的代码工作正常。但是,当我在Windows 2008R2服务器上运行相同的控制台应用程序时,它的作用不同。
创建快捷方式修改目标路径
所以,我有我的控制台应用程序,并会出现以下情况:
我的程序简单,配有的.docx扩展在桌面上创建快捷方式和我的本地机器上的一切都很好。当我在我的服务器上运行相同的控制台应用程序时,它会创建相同的快捷方式,但目标已修改为 ...它将目标更改为.doc文件。
换句话说,当我运行控制台应用程序:
LOCALMACHINE
创建MyWordFile.lnk指着U:\ test.docx
服务器
创建MyWordFile.lnk指着U:\ Test.doc的
这是奇怪的行为。下面是代码
代码
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
namespace TestShortcut
{
class Program
{
static void Main(string[] args)
{
//ShortcutTarget
var sTarget = @"U:\test.docx";
//ShortCutName
var sName = @"MyWordFile.lnk";
IShellLink link = (IShellLink)new ShellLink();
link.SetPath(sTarget);
IPersistFile file = (IPersistFile)link;
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
file.Save(Path.Combine(desktopPath, sName), false);
}
}
[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);
}
}
更新
我注意到4字符被从目标的扩展删除。所以,当我有一个文件“file.abcdef”,链接指向“file.abc”。此外空间会被替换为下划线,所以“我file.abcd”指针变成“my_file.abc”
非常感谢,非常清楚的解释! –