C#带参数运行方法

比如
aa.exe -auto
aa.exe -main
两组后缀,要求分别运行aa的某个线程,比如aa.exe -auto打开from1,aa.exe -main打开from2
由于需要修改Program的Main方法,需要更加谨慎,因为一个结构清晰的Main对于后期维护是一个很好的帮助。以下的代码将解析参数,构造启动窗体,启动窗体三个逻辑分割为三个方法

Code
 1 static class Program
 2     {
 3         /// <summary>
 4         /// The main entry point for the application.
 5         /// </summary>
 6         [STAThread]
 7         static void Main(string[] Args)
 8         {
 9 
10             Application.EnableVisualStyles();
11             Application.SetCompatibleTextRenderingDefault(false);
12             //启动有默认启动窗体构造器构造出来的启动窗体
13 Application.Run(StartFormCreator(ParseArgsForFormlabel(Args)));
14         }
15 
16 //从参数中解析启动窗体参数
17         static string ParseArgsForFormlabel(string[] args)
18         {
19             string formLable = string.Empty;
20 //如果参数数量大于0则截取第一个参数,否则返回值为string.Empty
21             if (args.Length > 0)
22             {
23                 formLable = args[0];
24             }
25             return formLable;
26         }
27 //根据启动窗体参数构造对应的窗体
28         static Form StartFormCreator(string Label)
29         {
30 //如果参数是-auto则构造Form1,否则为Form2
31             if (Label.ToLower() == "-auto")
32             {
33                 return new Form1();
34             }
35             else
36             {
37                 return new Form2();
38             }
39         }
40     } 

时间: 2024-07-28 22:10:28

C#带参数运行方法的相关文章

关于VB中带参数运行EXE文件的问题

问题描述 我想点击一个按钮的时候执行一个EXE文件.Shell("rundll32.exeurl.dll,FileProtocolHandler"&Application.StartupPath&"lin.exe",vbMaximizedFocus)我用这种方法现在已经可以成功运行EXE文件了现在我想带参数运行这个EXE文件,应该真么写譬如我想运行的是lin.exe/s127.0.0.1应该怎么写我已经试过把lin.exe直接换成lin.exe/s1

C#线程调用带参数的方法

在 .NET Framework 2.0 版中,要实现线程调用带参数的方法有两种办法. 第一种:使用ParameterizedThreadStart. 调用 System.Threading.Thread.Start(System.Object) 重载方法时将包含数据的对象传递给线程. 使用 ParameterizedThreadStart 委托不是传递数据的类型安全的方法,因为 System.Threading.Thread.Start(System.Object) 方法重载接受任何对象. 这

C#创建线程带参数的方法_C#教程

1.无参数线程的创建 Thread thread = new Thread(new ThreadStart(getpic)); thread.Start(); private void showmessage() { Console.WriteLine("hello world"); } 2.带一个参数的线程 使用ParameterizedThreadStart,调用 System.Threading.Thread.Start(System.Object) 重载方法时将包含数据的对象传

ASP代码实现301重定向及带参数的方法

  由于改版需要,烈火网的一个栏目需要做301重定向,这是很久以前的烈火导航下的一个搜索,使用的是asp语言,但是蜘蛛喜欢,因此不能删除,只好写一个301重定向,但是原来的很多网址都是有参数的,例如TAG标签,形式如:liehuo_tag.asp?q=%C1%D2%BB%F0%CD%F8. 研究了一下,解决了301重定向带参数的问题,特来向大家分享,欢迎朋友多支持烈火网. 代码如下: <% if request.ServerVariables("HTTP_HOST")="

在命令行下运行PHP脚本[带参数]的方法_php技巧

创建一个简单的文本文件,其中包含有以下PHP代码,并把它保存为hello.php: 复制代码 代码如下: <?php echo "Hello from the CLI"; ?> 现在,试着在命令行提示符下运行这个程序,方法是调用CLI可执行文件并提供脚本的文件名: #php phphello.php 输出Hello from the CLI 使用标准的输入和输出 你可以在自己的PHP脚本里使用这三个常量,以接受用户的输入,或者显示处理和计算的结果.要更好地理解这一点,可以看

ObjectDataSource控件的select方法如何应用类似select(sql)带参数的方法

问题描述 比如我有一个Result类,其中有一个Result.SearchResult(strSQL)的select方法,这个类还有更新,删除方法,我现在已经指定了更新,删除方法,而且Result.SearchResult(strSQL)也以指定,其中的strSql参数指定为None,strSQL是通过组合选择拼接而成的,我想让ObjectDataSource在程序中指定strSQL,完整的代码如何写?this.ResutlObjectDataSource.SelectParameters["s

C#,往线程里传带参数的方法

问题描述 List<SyncTableInfo>listTables=GetSyncTables();listTables=listTables.OrderBy(n=>n.Sort).ToList();string[]dataSource=new[]{"etl_scn","etl_aisien","erp-xy"};Thread[]thread=newThread[dataSource.Count()];for(inti=0;i

php include()带参数实现方法

具体方法详解 举例:假设在 index.php 中需要调用 inc.php?para=3 , inc.php  代码如下 复制代码 <?php     echo $_GET['para']; ?> 下面的写法是无法得到正确结果的: index.php  代码如下 复制代码 <?php     include dirname(__FILE__).'/inc.php?para=3'; ?> 稍微变通一下,把$_GET变量在include之前定义,则可以正常运行: index.php  

Python - 带参数的方法

import math class Point: def move(self, x, y): self.x = x self.y = y def reset(self): self.move(0, 0) def calculate_distance(self, other_point): return math.sqrt( (self.x - other_point.x)**2 + (self.y - other_point.y)**2) # how to use it: point1 = Po