C# 如何在表单应用程序中获取参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1241440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get arguments in a form application?
提问by Moon
I can find many examples on how to get arguments in a console application, but I can't seem to find an example of how to get arguments in a windows form application.
我可以找到很多关于如何在控制台应用程序中获取参数的示例,但我似乎无法找到如何在 Windows 窗体应用程序中获取参数的示例。
I would like to following things.
我想关注的事情。
- whenever I open a jpg file, windows launches my application.
- I would like to know path and name of the jpg file from my application.
- 每当我打开一个 jpg 文件时,Windows 都会启动我的应用程序。
- 我想知道我的应用程序中 jpg 文件的路径和名称。
How do i do that?
我怎么做?
回答by Allen Rice
Open up program.cs, on a file > new > winform project, you'll get
打开program.cs,在文件>新建>winform项目上,你会得到
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
change this to
将此更改为
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Now its just like the console apps, you'd access them via args.
现在它就像控制台应用程序一样,您可以通过 args 访问它们。
Even if you don't go with this option, you should be aware of how the win form app is initialized :) This way, you could run different forms or not run a form at all.
即使您不使用此选项,您也应该了解 win 表单应用程序是如何初始化的 :) 这样,您可以运行不同的表单或根本不运行表单。