如何通过 windows 命令创建文件 link 并在该 link 的目录中定义开始?
How to create a file link through windows command and define start in directory for that link?
我需要为我的应用程序创建一个快捷方式,并且快捷方式需要具有相同的图标,因此不考虑 bat 文件而不是快捷方式。我也想要一个原生的 windows 解决方案或 .NET 5.0 解决方案,而不是第三方程序,我希望尽可能低到源代码。
我试过 mklink,但它没有提供设置快捷方式“开始于:”目录的选项,这对我需要创建快捷方式的应用程序至关重要。
这是我为自己做的:
public class DesktopUtility : IDesktopUtility
{
public void CreateShortcut(string targetPath, string shortcutLinkPath)
{
if (!shortcutLinkPath.EndsWith(".lnk"))
shortcutLinkPath += ".lnk";
CreateShortcutVBS(targetPath, shortcutLinkPath);
if (!File.Exists(shortcutLinkPath))
CreateShortcutPS(targetPath, shortcutLinkPath);
}
public void CreateShortcutVBS(string targetPath, string shortcutLinkPath)
{
if (!shortcutLinkPath.EndsWith(".lnk"))
shortcutLinkPath += ".lnk";
var workingDirectory = Path.GetDirectoryName(targetPath);
var vbShortcutScript = "Set oWS = WScript.CreateObject(\"WScript.Shell\")\n" +
$"sLinkFile = \"{shortcutLinkPath}\"\n" +
"Set oLink = oWS.CreateShortcut(sLinkFile) \n" +
$"oLink.TargetPath = \"{targetPath}\"\n" +
$"oLink.WorkingDirectory = \"{workingDirectory}\"\n" +
"oLink.Save";
var fileName = Path.GetFileNameWithoutExtension(targetPath);
var scriptFilePath = Path.Combine(workingDirectory, $"{fileName}.vbs");
//var wscriptPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"System32\wscript.exe");
try
{
using (var file = File.CreateText(scriptFilePath))
file.Write(vbShortcutScript);
var psi = new ProcessStartInfo
{
FileName = "wscript.exe",
UseShellExecute = false,
Arguments= $"/b \"{scriptFilePath}\""
};
Process.Start(psi).WaitForExit();
}
finally
{
if (File.Exists(scriptFilePath))
File.Delete(scriptFilePath);
}
}
public void CreateShortcutPS(string targetPath, string shortcutLinkPath)
{
if (!shortcutLinkPath.EndsWith(".lnk"))
shortcutLinkPath += ".lnk";
var workingDirectory = Path.GetDirectoryName(targetPath);
var psi = new ProcessStartInfo
{
FileName = "powershell.exe",
WindowStyle = ProcessWindowStyle.Hidden
};
psi.Arguments =
"-windowstyle hidden " +
"$WshShell=New-Object -comObject WScript.Shell; " +
$"$LinkPath = \\"{shortcutLinkPath}\\"; " +
$"$Shortcut = $WshShell.CreateShortcut($LinkPath); " +
$"$Shortcut.TargetPath = \\"{ targetPath}\\"; " +
$"$Shortcut.WorkingDirectory = \\"{workingDirectory}\\"; " +
"$Shortcut.Save();";
Process.Start(psi).WaitForExit();
}
}
我不喜欢 PowerShell 方法,因为它简要显示了 window,我希望能够内联执行 vbscript,而不是通过文件,不知道如何。
我需要为我的应用程序创建一个快捷方式,并且快捷方式需要具有相同的图标,因此不考虑 bat 文件而不是快捷方式。我也想要一个原生的 windows 解决方案或 .NET 5.0 解决方案,而不是第三方程序,我希望尽可能低到源代码。
我试过 mklink,但它没有提供设置快捷方式“开始于:”目录的选项,这对我需要创建快捷方式的应用程序至关重要。
这是我为自己做的:
public class DesktopUtility : IDesktopUtility
{
public void CreateShortcut(string targetPath, string shortcutLinkPath)
{
if (!shortcutLinkPath.EndsWith(".lnk"))
shortcutLinkPath += ".lnk";
CreateShortcutVBS(targetPath, shortcutLinkPath);
if (!File.Exists(shortcutLinkPath))
CreateShortcutPS(targetPath, shortcutLinkPath);
}
public void CreateShortcutVBS(string targetPath, string shortcutLinkPath)
{
if (!shortcutLinkPath.EndsWith(".lnk"))
shortcutLinkPath += ".lnk";
var workingDirectory = Path.GetDirectoryName(targetPath);
var vbShortcutScript = "Set oWS = WScript.CreateObject(\"WScript.Shell\")\n" +
$"sLinkFile = \"{shortcutLinkPath}\"\n" +
"Set oLink = oWS.CreateShortcut(sLinkFile) \n" +
$"oLink.TargetPath = \"{targetPath}\"\n" +
$"oLink.WorkingDirectory = \"{workingDirectory}\"\n" +
"oLink.Save";
var fileName = Path.GetFileNameWithoutExtension(targetPath);
var scriptFilePath = Path.Combine(workingDirectory, $"{fileName}.vbs");
//var wscriptPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"System32\wscript.exe");
try
{
using (var file = File.CreateText(scriptFilePath))
file.Write(vbShortcutScript);
var psi = new ProcessStartInfo
{
FileName = "wscript.exe",
UseShellExecute = false,
Arguments= $"/b \"{scriptFilePath}\""
};
Process.Start(psi).WaitForExit();
}
finally
{
if (File.Exists(scriptFilePath))
File.Delete(scriptFilePath);
}
}
public void CreateShortcutPS(string targetPath, string shortcutLinkPath)
{
if (!shortcutLinkPath.EndsWith(".lnk"))
shortcutLinkPath += ".lnk";
var workingDirectory = Path.GetDirectoryName(targetPath);
var psi = new ProcessStartInfo
{
FileName = "powershell.exe",
WindowStyle = ProcessWindowStyle.Hidden
};
psi.Arguments =
"-windowstyle hidden " +
"$WshShell=New-Object -comObject WScript.Shell; " +
$"$LinkPath = \\"{shortcutLinkPath}\\"; " +
$"$Shortcut = $WshShell.CreateShortcut($LinkPath); " +
$"$Shortcut.TargetPath = \\"{ targetPath}\\"; " +
$"$Shortcut.WorkingDirectory = \\"{workingDirectory}\\"; " +
"$Shortcut.Save();";
Process.Start(psi).WaitForExit();
}
}
我不喜欢 PowerShell 方法,因为它简要显示了 window,我希望能够内联执行 vbscript,而不是通过文件,不知道如何。