C#: I want to pass messages like a file path to my forms application like a console application, how would I do that?
C#:我想像文件路径一样将消息传递给我的表单应用程序,就像控制台应用程序一样,我该怎么做?
I was told I needed to find my main method to add string[] args, but I wouldn't know which one that would be in Windows Forms. Which would my main method be in C# windows forms application?
我被告知我需要找到我的主要方法来添加string [] args,但我不知道哪个将在Windows窗体中。我的主要方法是哪种C#windows窗体应用程序?
5 个解决方案
#1
Ok, string[] args = Environment.GetCommandLineArgs() is a better option. But I will keep the following answer as an alternative to it.
好的,string [] args = Environment.GetCommandLineArgs()是一个更好的选择。但我会保留以下答案作为替代方案。
Look for a file called Program.cs containing the following code fragment...
查找名为Program.cs的文件,其中包含以下代码片段...
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
and change that to
并改为
static class Program
{
public static string[] CommandLineArgs { get; private set;}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
CommandLineArgs = args;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Then access the command line args from your form ...
然后从表单中访问命令行args ...
Program.CommandLineArgs
#2
Your Main()
method is located in Program.cs
file, typically like this:
您的Main()方法位于Program.cs文件中,通常如下所示:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
You should modify the Main()
to the following:
您应该将Main()修改为以下内容:
static void Main(string[] args)
You'll have access to the arguments passed.
您将有权访问传递的参数。
Also, you could access the arguments using Environment.GetCommandLineArgs()
此外,您可以使用Environment.GetCommandLineArgs()访问参数
#3
If you want to get access to the command line parameters, use Environment.CommandLine
如果要访问命令行参数,请使用Environment.CommandLine
string args = Environment.CommandLine;
You can do this whether or not you have a main method with string[] args in your code.
无论你的代码中是否有一个带有string [] args的main方法,都可以这样做。
#4
There's one Main()
, which is inside Program.cs
. But in WinForms app Environment.GetCommandLineArgs()
will be a better option.
有一个Main(),它位于Program.cs中。但是在WinForms应用环境中,Environment.GetCommandLineArgs()将是一个更好的选择。
#5
in your public constructor, use the following:
在您的公共构造函数中,使用以下内容:
string[] args = Environment.GetCommandLineArgs();
string [] args = Environment.GetCommandLineArgs();
this will give you a string array of the arguments.
这将为您提供参数的字符串数组。
#1
Ok, string[] args = Environment.GetCommandLineArgs() is a better option. But I will keep the following answer as an alternative to it.
好的,string [] args = Environment.GetCommandLineArgs()是一个更好的选择。但我会保留以下答案作为替代方案。
Look for a file called Program.cs containing the following code fragment...
查找名为Program.cs的文件,其中包含以下代码片段...
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
and change that to
并改为
static class Program
{
public static string[] CommandLineArgs { get; private set;}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
CommandLineArgs = args;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Then access the command line args from your form ...
然后从表单中访问命令行args ...
Program.CommandLineArgs
#2
Your Main()
method is located in Program.cs
file, typically like this:
您的Main()方法位于Program.cs文件中,通常如下所示:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
You should modify the Main()
to the following:
您应该将Main()修改为以下内容:
static void Main(string[] args)
You'll have access to the arguments passed.
您将有权访问传递的参数。
Also, you could access the arguments using Environment.GetCommandLineArgs()
此外,您可以使用Environment.GetCommandLineArgs()访问参数
#3
If you want to get access to the command line parameters, use Environment.CommandLine
如果要访问命令行参数,请使用Environment.CommandLine
string args = Environment.CommandLine;
You can do this whether or not you have a main method with string[] args in your code.
无论你的代码中是否有一个带有string [] args的main方法,都可以这样做。
#4
There's one Main()
, which is inside Program.cs
. But in WinForms app Environment.GetCommandLineArgs()
will be a better option.
有一个Main(),它位于Program.cs中。但是在WinForms应用环境中,Environment.GetCommandLineArgs()将是一个更好的选择。
#5
in your public constructor, use the following:
在您的公共构造函数中,使用以下内容:
string[] args = Environment.GetCommandLineArgs();
string [] args = Environment.GetCommandLineArgs();
this will give you a string array of the arguments.
这将为您提供参数的字符串数组。