cmd里能调用C#的exe然后传递参数过去么?

zoroJean 2012-03-13 03:45:39
如题。想通过cmd运行一个exe。但是需要传递参数过去。需要怎么写CMD命令?
...全文
689 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gzw13999 2012-03-13
  • 打赏
  • 举报
回复
你是要给你c# 程序加运行参数?



来自于搜索引擎

由于需要修改Program的Main方法,需要更加谨慎,因为一个结构清晰的Main对于后期维护是一个很好的帮助。以下的代码将解析参数,构造启动窗体,启动窗体三个逻辑分割为三个方法。

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] Args)
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//启动有默认启动窗体构造器构造出来的启动窗体
Application.Run(StartFormCreator(ParseArgsForFormlabel(Args)));
}

//从参数中解析启动窗体参数
static string ParseArgsForFormlabel(string[] args)
{
string formLable = string.Empty;
//如果参数数量大于0则截取第一个参数,否则返回值为string.Empty
if (args.Length > 0)
{
formLable = args[0];
}
return formLable;
}
//根据启动窗体参数构造对应的窗体
static Form StartFormCreator(string Label)
{
//如果参数是-auto则构造Form1,否则为Form2
if (Label.ToLower() == "-auto")
{
return new Form1();
}
else
{
return new Form2();
}
}
}

threenewbee 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 nonocast 的回复:]
或者用PowerShell,可以直接调用C#的Class.Method
[/Quote]
有点类似 rundll 的思路。原来有人提过写一个 runmanagedll
nonocast 2012-03-13
  • 打赏
  • 举报
回复
或者用PowerShell,可以直接调用C#的Class.Method
threenewbee 2012-03-13
  • 打赏
  • 举报
回复
C#程序使用Main() 的Args参数接收。
欢乐的小猪 2012-03-13
  • 打赏
  • 举报
回复
开始--运行--cmd--回车--
输入 exe名称.exe 参数1 参数2
然后回车
gxingmin 2012-03-13
  • 打赏
  • 举报
回复
using System.Diagnostics;

ProcessStartInfo info=new ProcessStartInfo(exe程序路径);
info.Arguments=参数1 + " " + 参数2 + " " + 参数n;
Process.Start(info);

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧