Click here to Skip to main content
16,008,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a requirement such that there is a function in my c#windows application arguments to that function should get from command prompt and then execute the program .how to do that??


thanks in advance
Posted

If you are using a console application, the main method woould have a string array as a parameter that would contain all the parameters.

E.g. The code below displays all the command line arguments.
class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}
 
Share this answer
 
Comments
narutoluffy01 6-Feb-12 5:42am    
i am using windows forms application
Espen Harlinn 6-Feb-12 8:45am    
5'ed! - args can be passed to the form constructor :)
narutoluffy01 6-Feb-12 23:07pm    
can u please give me some sort of example..:)
Abhinav S 7-Feb-12 0:57am    
I have already put my source code here.
None the less - see http://www.blackwasp.co.uk/WindowsFormsStartParams.aspx
C#
string[] System.Environment.GetCommandLineArgs()
 
Share this answer
 
The arguments to Main are the command line arguments. If you're using the standard Visual Studio template, you can find the Main method in Program.cs. If I need them in a Windows application I generally modify that Main method to pass the array of arguments into the main form's constructor, and handle them in there.

Ralphlee1's answer is also correct but I prefer to pass them as arguments from the single entry point because that seems cleaner, as the external information is only being fed in in a single place.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900