将快捷方式中的信息传递到c#.exe
本文关键字:exe 信息 快捷方式 | 更新日期: 2023-09-27 17:58:59
我有一个wpf c#项目,我想通过查看其他程序员的快捷来传递一些启动信息
所以我看到的捷径是:
X: ''Test.exe/host=[服务器位置]/实例=0/coid=%coid/userid=%operat
我理解正在传递的内容,但我想了解c#项目是如何接收粗体信息的,我猜是如何将其分配到字符串等中的。
我试着用谷歌搜索信息,但我不知道该怎么称呼这个话题
任何帮助-即使是不这样做也会有帮助
请参阅MSDN上的命令行参数教程。
应用程序有一个入口点,在本例中为public static void Main(string[] args)
。args
参数包含按空格分隔的命令行参数。
编辑:我的坏,不知道WPF是令人讨厌的。看看这里:WPF:支持命令行参数和文件扩展名:
protected override void OnStartup(StartupEventArgs e)
{
if (e.Args != null && e.Args.Count() > 0)
{
this.Properties["ArbitraryArgName"] = e.Args[0];
}
base.OnStartup(e);
}
您可以使用将命令行参数检索到string[]
中
string[] paramz = Environment.GetCommandLineArgs();
在本例中,程序在运行时获取一个参数,将该参数转换为整数,并计算该数字的阶乘。如果没有提供任何参数,程序将发出一条消息,解释程序的正确用法。
public class Functions
{
public static long Factorial(int n)
{
if (n < 0) { return -1; } //error result - undefined
if (n > 256) { return -2; } //error result - input is too big
if (n == 0) { return 1; }
// Calculate the factorial iteratively rather than recursively:
long tempResult = 1;
for (int i = 1; i <= n; i++)
{
tempResult *= i;
}
return tempResult;
}
}
class MainClass
{
static int Main(string[] args)
{
// Test if input arguments were supplied:
if (args.Length == 0)
{
System.Console.WriteLine("Please enter a numeric argument.");
System.Console.WriteLine("Usage: Factorial <num>");
return 1;
}
try
{
// Convert the input arguments to numbers:
int num = int.Parse(args[0]);
System.Console.WriteLine("The Factorial of {0} is {1}.", num, Functions.Factorial(num));
return 0;
}
catch (System.FormatException)
{
System.Console.WriteLine("Please enter a numeric argument.");
System.Console.WriteLine("Usage: Factorial <num>");
return 1;
}
}
}
输出将是The Factorial of 3 is 6.
,此应用程序的用法类似于Factorial.exe <num>