如何从快捷方式属性的Target中获取参数并在C# WPF应用中读取该参数值并赋值给属性?
没问题,我帮你拆解这两个问题,一步步来解决:
一、从Windows快捷方式的Target中提取参数
1. 手动提取(直接查看快捷方式属性)
- 右键快捷方式,选择「属性」
- 切换到「快捷方式」标签页,找到「目标」输入框
- 在里面定位到格式为
-p"参数内容"的片段,引号中间的部分就是你要的SomeParam;如果参数没有引号,格式是-p 参数内容,直接取-p后面的内容即可
2. 用C#代码读取快捷方式并提取参数
Windows快捷方式是.lnk格式的文件,要读取它的Target路径需要借助Shell COM组件:
- 先在项目里添加引用:右键项目→「添加引用」→「COM」→找到「Microsoft Shell Controls And Automation」勾选确定
- 然后用下面的代码实现:
using Shell32; using System.Text.RegularExpressions; // 读取快捷方式的完整Target(路径+参数) public string GetShortcutFullTarget(string shortcutFilePath) { Shell shellInstance = new Shell(); var folder = shellInstance.NameSpace(Path.GetDirectoryName(shortcutFilePath)); var shortcutItem = folder.ParseName(Path.GetFileName(shortcutFilePath)); if (shortcutItem != null) { var shellLink = (ShellLinkObject)shortcutItem.GetLink; // 拼接路径和参数 return $"{shellLink.Target.Path} {shellLink.Arguments}"; } return string.Empty; } // 从Target字符串里提取-p的参数值 public string ExtractParamValue(string targetContent) { // 先匹配带引号的格式:-p"SomeParam" var quotedMatch = Regex.Match(targetContent, @"-p""([^""]+)"""); if (quotedMatch.Success) { return quotedMatch.Groups[1].Value; } // 再匹配不带引号的格式:-p SomeParam 或-pSomeParam var plainMatch = Regex.Match(targetContent, @"-p\s*(\S+)"); return plainMatch.Success ? plainMatch.Groups[1].Value : string.Empty; }
二、C# WPF应用启动时读取参数并设置属性
当用户通过带参数的快捷方式启动你的WPF应用时,这些参数会作为命令行参数传递给程序,你可以在应用启动阶段读取并处理:
方法1:使用Environment.GetCommandLineArgs()
这个方法会返回包含程序路径和所有参数的数组,第一个元素是程序本身的路径,后面的是传递的参数:
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); string targetParam = string.Empty; string[] allArgs = Environment.GetCommandLineArgs(); foreach (string arg in allArgs) { if (arg.StartsWith("-p")) { // 处理-p"SomeParam"的情况 if (arg.StartsWith("-p\"")) { targetParam = arg.Substring(3, arg.Length - 4); } // 处理-pSomeParam或者-p SomeParam的情况 else { // 如果参数和-p连在一起,直接截取后面的内容 targetParam = arg.Substring(2); // 检查是否是-p和参数分开的情况(比如参数是下一个数组元素) int argIndex = Array.IndexOf(allArgs, arg); if (argIndex < allArgs.Length - 1 && !allArgs[argIndex + 1].StartsWith("-")) { targetParam = allArgs[argIndex + 1]; } } break; } } // 将参数设置到目标属性,比如MainWindow的MyParam属性 var mainWindow = new MainWindow(); mainWindow.MyParam = targetParam; mainWindow.Show(); } }
方法2:使用StartupEventArgs.Args
在OnStartup方法里,e.Args直接返回所有命令行参数(不包含程序路径),用法更简洁:
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); string targetParam = string.Empty; for (int i = 0; i < e.Args.Length; i++) { string currentArg = e.Args[i]; // 处理-p和参数分开的情况:-p SomeParam if (currentArg.Equals("-p", StringComparison.OrdinalIgnoreCase)) { if (i < e.Args.Length - 1) { targetParam = e.Args[i + 1]; break; } } // 处理-p"SomeParam"或-pSomeParam的情况 else if (currentArg.StartsWith("-p")) { targetParam = currentArg.Substring(2); // 去掉引号(如果有) if (targetParam.StartsWith("\"") && targetParam.EndsWith("\"")) { targetParam = targetParam.Substring(1, targetParam.Length - 2); } break; } } // 设置到目标属性 var mainWindow = new MainWindow(); mainWindow.MyParam = targetParam; mainWindow.Show(); } }
注:如果你的WPF应用里没有显式重写OnStartup,可以在App.xaml里去掉StartupUri,然后手动在OnStartup里创建并显示主窗口。
内容的提问来源于stack exchange,提问作者DeveloperLV
相关产品推荐
相关产品推荐

