By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,574 Members | 1,535 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,574 IT Pros & Developers. It's quick & easy.

Execute Windows application from Console

P: 2
Hi,

I have created a Windows application (in VB .NET). Now I want it to run from Console too. However, when i run it from console, i don't want to should the windows forms and also want to run some specific tasks.
Jun 10 '10 #1
Share this Question
Share on Google+
1 Reply


P: 2
@virendrachandak
I got a solution for this.

Reference:
http://social.msdn.microsoft.com/For...-d38910bde613/
-----------------------
You can create a console window in a Windows Forms project. Project + properties, turn off "Enable application framework" and set Startup object to "Sub Main". Add a new module to your project and paste this code:

Module Module1
Private Declare Auto Function AllocConsole Lib "kernel32.dll" () As Boolean
Public Sub Main()
'--- Check if command line arguments were specified
Dim args() As String = Environment.GetCommandLineArgs()
If args.Length > 1 Then
'--- Yep, create the console window and run the console mode logic
AllocConsole()
ConsoleMain(args)
Else
'--- Nope, run the normal Windows Forms startup code
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(Fals e)
Application.Run(New Form1)
End If
End Sub

Private Sub ConsoleMain(ByVal args() As String)
'--- This code runs when command line arguments were given
Console.WriteLine("Command line = {0}", Environment.CommandLine)
Console.WriteLine("Exe path = {0}", args(0))
For ix As Integer = 1 To args.Length - 1
Console.WriteLine(" Argument({0}) = {1}", ix, args(ix))
Next
Console.Write("Press ENTER to continue")
Dim s As String = Console.ReadLine()
End Sub

End Module

Modify the Application.Run() statement to create the proper startup form, if necessary.
Jun 10 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.