![](/img/trans.png)
[英]how to get file location from user side in c# console application. I want full path from user side like "C:\Filepath"
[英]C#: I want to pass messages like a file path to my forms application like a console application, how would I do that?
C#:我想像文件路径一样将消息传递给我的表单应用程序,比如控制台应用程序,我该怎么做?
我被告知我需要找到我的主要方法来添加string [] args,但我不知道哪个是Windows窗体。 我的主要方法是哪种C#windows窗体应用程序?
好的,string [] args = Environment.GetCommandLineArgs()是一个更好的选择。 但我会保留以下答案作为替代方案。
查找名为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());
}
}
并改为
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());
}
}
然后从表单中访问命令行args ...
Program.CommandLineArgs
您的Main()
方法位于Program.cs
文件中,通常如下所示:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
您应该将Main()
修改为以下内容:
static void Main(string[] args)
您将有权访问传递的参数。
此外,您可以使用Environment.GetCommandLineArgs()
访问参数
如果要访问命令行参数,请使用Environment.CommandLine
string args = Environment.CommandLine;
无论你的代码中是否有一个带有string [] args的main方法,你都可以这样做。
有一个Main()
,它位于Program.cs
。 但在WinForms应用程序Environment.GetCommandLineArgs()
将是一个更好的选择。
在您的公共构造函数中,使用以下内容:
string [] args = Environment.GetCommandLineArgs();
这将为您提供参数的字符串数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.