惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
NISL@THU
NISL@THU
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tenable Blog
Forbes - Security
Forbes - Security
V2EX - 技术
V2EX - 技术
Webroot Blog
Webroot Blog
Schneier on Security
Schneier on Security
T
The Exploit Database - CXSecurity.com
T
Tor Project blog
C
Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
PCI Perspectives
PCI Perspectives
O
OpenAI News
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
D
Docker
AI
AI
Blog — PlanetScale
Blog — PlanetScale
S
Security @ Cisco Blogs
S
Schneier on Security
The GitHub Blog
The GitHub Blog
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
P
Privacy International News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog
C
Check Point Blog
A
About on SuperTechFans
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Engineering at Meta
Engineering at Meta
I
InfoQ
T
Threat Research - Cisco Blogs
Project Zero
Project Zero
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
Cisco Talos Blog
Cisco Talos Blog
L
Lohrmann on Cybersecurity
S
Securelist

博客园 - 光脚码农

CentOS安装JDK CentOS 7中安装和配置Promethues - 光脚码农 - 博客园 查看和指定SpringBoot内嵌Tomcat的版本 - 光脚码农 - 博客园 CentOS中安装Azkaban 2.5 Centos7 安装Nodejs SpringBoot实用技巧札记 SQL实用札记【SQL Sever篇】 话说静态构造函数 如何利用正则表达式匹配花括号内的内容 利用存储过程来重命名SQL Server数据库 .NET批量操作窗口样式 ViewData、ViewBag、TempData、Session的区别与联系 如何为自己的网页实现一个“回到顶部”的链接? 如何获得数据库中所有用户创建的索引 如何为一个类型为Color的属性设置默认值 用BCP从SQL Server 数据库中导出Excel文件 DataGridView如何绑定DataRow对象集合 Const vs. Readonly Windows下Git的安装与配置(Cygwin)
如何为Windows Forms应用程序添加启动参数(Start-Up Parameters)
光脚码农 · 2014-07-21 · via 博客园 - 光脚码农

很多场合下,我们需要通过命令行或者快捷方式在Windows Forms程序启动时向其传递参数。 这些参数可能是用来加载某一个文档,或者是应用程序的初始化配置文件。 特别是对那些需要高度自定义配置的大程序,经常需要调整运行参数来帮助使用者获得不同的运行结果。

通常,我们可以通过以下两种方式来实现这个需求:

  • 重载入口点函数(Main)
  • 利用Environment类

重载入口点函数(Main)

我们在Visual Studio中创建Windows Forms程序时, VS会自动帮我们创建一个默认的入口点方法——Main。

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

这个入口点函数基本上适用所有程序,但如果想要接收命令行参数,则必须用一个带参数的Main方法来替换它。我们可以简单的向Main方法添加一个string数组参数, 比如:

[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

现在,我们就可以通过这个string数组(args)的值来获取程序启动时传递进来的参数信息。 如果没有传递任何参数,则这个数组为空。否则,每一个参数对应数组中的一个元素,这些参数用空格符来分隔。

利用Environment类

第二种获取应用程序启动参数的方式就是使用Environment类。 我们可以在System命名空间下找到Environment类,类中包含一个叫GetCommandLineArgs()的方法。 此方法会返回一个string数组,而数组的第一个值是程序的文件名(这也是两种方法所获取的参数的区别)。 如果可用,则其他元素就是通过命令行传递过来的参数。

注: 命令行可以通过快捷方式来配置,比如:

示例程序

为了分别演示利用这两种方法来向向Windows Forms程序传递启动参数,我通过一个简单的程序来说明。

参考文献