我通常從不遇到編程問題,因為我可以輕松找到其中大部分問題的答案。但我在這個問題上不知所措。
在 Powershell 中創建 Windows 快捷方式的眾所周知的方法如下:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("F:\path\to\shortcut.lnk")
$Shortcut.TargetPath = "F:\some\other\path\to\targetfile.txt"
$Shortcut.Save()
但是,這種方法有一個缺點,我開始越來越困擾它:當檔案名包含特殊字符(例如檔案名中的笑臉 ??)時,它不起作用:
"targetfile ??.txt"
我研究了這個問題并在這里發現WshShortcut 物件和 WScript 不能接受檔案名中的 unicode。它似乎只適用于一組簡單的字符。當然,當您在 Windows 中右鍵單擊該檔案并選擇“創建快捷方式”時,Windows 創建帶有特殊字符的快捷方式是沒有問題的。
有人在 C# 中撰寫了一種使用 Shell32 創建快捷方式的替代方法,但我不知道是否可以在 Powershell 中完成。它看起來像是一種舊方法,可能不適用于較新的 Windows 版本。
有人可以幫我解決這個問題嗎?如何在 Powershell 中為檔案名中包含特殊字符的檔案創建 Windows 快捷方式?
uj5u.com熱心網友回復:
如有疑問,請使用 C#:
$ShellLinkCSharp = @'
namespace Shox
{
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
[CoClass(typeof(CShellLinkW))]
interface IShellLinkW
{
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, uint fFlags);
IntPtr GetIDList();
void SetIDList(IntPtr pidl);
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, 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);
ushort GetHotKey();
void SetHotKey(ushort wHotKey);
uint GetShowCmd();
void SetShowCmd(uint 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, [Optional] uint dwReserved);
void Resolve(IntPtr hwnd, uint fFlags);
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
[ClassInterface(ClassInterfaceType.None)]
class CShellLinkW { }
public static class ShellLink
{
public static void CreateShortcut(
string lnkPath,
string targetPath,
string description,
string workingDirectory)
{
if (string.IsNullOrWhiteSpace(lnkPath))
throw new ArgumentNullException("lnkPath");
if (string.IsNullOrWhiteSpace(targetPath))
throw new ArgumentNullException("targetPath");
IShellLinkW link = new IShellLinkW();
link.SetPath(targetPath);
if (!string.IsNullOrWhiteSpace(description))
{
link.SetDescription(description);
}
if (!string.IsNullOrWhiteSpace(workingDirectory))
{
link.SetWorkingDirectory(workingDirectory);
}
IPersistFile file = (IPersistFile)link;
file.Save(lnkPath, true);
Marshal.FinalReleaseComObject(link);
}
}
}
'@
# Check if Shox.ShellLink class already exists; if not, import it:
if (-not ([System.Management.Automation.PSTypeName]'Shox.ShellLink').Type)
{
Add-Type -TypeDefinition $ShellLinkCSharp
}
[Shox.ShellLink]::CreateShortcut(
'F:\path\to\shortcut1.lnk',
'F:\some\other\path\to\targetfile1 ??.txt',
'???? my description ????',
'F:\some\another\path\my_working_directory')
[Shox.ShellLink]::CreateShortcut(
'F:\path\to\shortcut2.lnk',
'F:\some\other\path\to\targetfile2 ??.txt',
$null, # No description
$null) # No working directory
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/369877.html
標籤:视窗 电源外壳 捷径 wscript.shell
