C#: I want to pass messages like a file path to my forms application like a console application, how would I do that?
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?
-
If you want to get access to the command line parameters, use Environment.CommandLine
string args = Environment.CommandLine;You can do this whether or not you have a main method with string[] args in your code.
-
There's one
Main(), which is insideProgram.cs. But in WinForms appEnvironment.GetCommandLineArgs()will be a better option. -
in your public constructor, use the following:
string[] args = Environment.GetCommandLineArgs();
this will give you a string array of the arguments.
-
Ok, string[] args = Environment.GetCommandLineArgs() is a better option. But I will keep the following answer as an alternative to it.
Look for a file called Program.cs containing the following code fragment...
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 ...
Program.CommandLineArgs -
Your
Main()method is located inProgram.csfile, typically like this:[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }You should modify the
Main()to the following:static void Main(string[] args)You'll have access to the arguments passed.
Also, you could access the arguments using
Environment.GetCommandLineArgs() -
hey guise when i do so with my application i get the following string "D:\MyData~1\MyMain~1.pfb" and it supposed to be like that "D:\My Data\My Main Phone Book.pbf" and i don't know why pls any bodyhas asolution to this
Matt Ellen : Hi falcon eyes, you need to ask this as a separate question - i.e. as its own question. be sure to give an example of the code you're running and the output you're getting.
0 comments:
Post a Comment