Solved

How to get my Console.exe to run as hidden?

Posted on 2010-09-14
8
523 Views
Last Modified: 2013-11-05
I have a very basic console app that is ment to collect users inventory in their "My Documents" & "My Desktop".
I would like to console app to run in a hidden state.
I did varous searches on the network, but I am not having any luck.

If you need to code I will post it if requested.
 
0
Comment
Question by:yo_bee
8 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33675611
You probably want your application to be a Windows Forms program, just don't show any forms.
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33675680
Like the first comment said, do it in a Win Forms app without any forms. Just Add a module and place all the code in sub Main.
0
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33675712
Set objShell = CreateObject("Wscript.Shell")
strCommand = "console.exe /param"
objShell.Run strCommand, 0, True


Using the '0' here hides the window.  "True" makes the script wait until the command is completed before resuming the script
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 33675719
Right-click on your project and go to Properties, turn off the "Enable Application Framework", set "Startup Object" to Sub Main().  Add a module to your project with a sub main() and this will be your starting point...
Here's a simple example that uses a custom application context to continuously loop in a  background thread.

Imports System.Threading

Module Module1

    Sub Main()

        Application.Run(New MyAppContext())

    End Sub

End Module

Public Class MyAppContext

    Inherits ApplicationContext

    Private worker As Thread

    Private exitWorkerSignal As ManualResetEvent

    Public Sub New()

        worker = New Thread(AddressOf WorkerProc)

        worker.IsBackground = True



        exitWorkerSignal = New ManualResetEvent(True)



        worker.Start()

    End Sub

    Private Sub WorkerProc()

        'Loop continuously, until the exitWorkerSignal is reset, then exit

        While (exitWorkerSignal.WaitOne(0))

            'Inventory my documents

            GetMyDocsInventory()



            'Sleep for a bit

            Thread.Sleep(500)

        End While

    End Sub

    Private Sub GetMyDocsInventory()

        'Dummy sub - do work here

    End Sub

    Protected Overrides Sub ExitThreadCore()

        exitWorkerSignal.Reset()

        MyBase.ExitThreadCore()

    End Sub

End Class

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Author Comment

by:yo_bee
ID: 33676103
Attached is my code modified to resemble the code above.
I am getting two errors.

Error      1      Name 'Application' is not declared.      C:\Users\boscam\AppData\Local\Temporary Projects\FileInventory\Module1.vb      8      9      FileInventory
Error      2      Type 'ApplicationContext' is not defined.      C:\Users\boscam\AppData\Local\Temporary Projects\FileInventory\Module1.vb      12      14      FileInventory

Note sure what this is?



Imports System

Imports System.IO

Imports System.Threading



Module Module1

    Sub Main()

        Application.Run(New MyAppContext())

    End Sub

End Module

Public Class MyAppContext

    Inherits ApplicationContext

    Private worker As Thread

    Private exitWorkerSignal As ManualResetEvent

    Public Sub New()

        worker = New Thread(AddressOf WorkerProc)

        worker.IsBackground = True



        exitWorkerSignal = New ManualResetEvent(True)



        worker.Start()

    End Sub

    Private Sub WorkerProc()

        'Loop continuously, until the exitWorkerSignal is reset, then exit

        While (exitWorkerSignal.WaitOne(0))

            'Inventory my documents

            GetMyDocsInventory()



            'Sleep for a bit

            Thread.Sleep(500)

        End While

    End Sub

    'Private Sub GetMyDocsInventory()

    'Dummy sub - do work here

    '********************************************************************************************* 

    Private Sub GetMyDocsInventory()

        Dim Computername As String = Environment.MachineName

        Dim StrFiles(0 To 1) As String

        StrFiles(0) = (My.Computer.FileSystem.SpecialDirectories.MyDocuments)

        StrFiles(1) = (My.Computer.FileSystem.SpecialDirectories.Desktop)



        Dim d As Double







        Using sw As StreamWriter = New StreamWriter(StrFiles(0) & "\" & Computername & ".txt", append:=False) ', False, System.Text.Encoding.Unicode)



            sw.WriteLine("Computer Name" & vbTab & "Directory Name" & vbTab & "File Name" & vbTab & "File Size" & vbTab & "File Extension")





            'Root Directory Starts here

            For Each StrDir In StrFiles

                'sw.WriteLine(vbtab & StrDir)



                FolderSearch(StrDir, sw, Computername, d)

            Next

            sw.Close()

            If File.Exists("\\flhappc\sys\networkinventory\" & "\" & Computername & ".txt") Then

                File.Delete("\\flhappc\sys\networkinventory\" & "\" & Computername & ".txt")

            End If

            File.Move(StrFiles(0) & "\" & Computername & ".txt", "\\flhappc\sys\networkinventory\" & "\" & Computername & ".txt")

        End Using





    End Sub



    Private Sub FolderSearch(ByVal startingDir As String, ByVal sw As StreamWriter, ByVal computername As String, ByVal d As Double)

        'This maybe used later if I am able to figure how to pass the expression 

        'to the getdirectories

        '

        'Dim RegexObj1 As New Regex("[0-9]")

        '******************************************

        Dim StartDI As New DirectoryInfo(startingDir)

        Dim SubFI As FileInfo() = StartDI.GetFiles()

        Dim SubDI As DirectoryInfo() = StartDI.GetDirectories()

        'Dim filesize As Double



        ' do files first

        'MsgBox(fsize)

        'Application.DoEvents()

        'If (StartDI.Attributes) <> 48 Then



        For Each SubFItemp In SubFI



            Try

                'fsize = ((((SubFItemp.Length) / 1024) / 1024) / 1024))



                sw.WriteLine(computername & vbTab & SubFItemp.DirectoryName & vbTab & SubFItemp.Name & vbTab & SubFItemp.Length & vbTab & SubFItemp.Extension)



            Catch ex As Exception

                sw.WriteLine(ex.Message)

            End Try

        Next



        'End If



        ' do the subdirectories, and then descend into the directory structure





        For Each SubDITemp In SubDI

            'If (SubDITemp.Attributes) <> 48 Then

            'sw.WriteLine()

            d = d + 1

            ' a recursive call to this routine

            FolderSearch(SubDITemp.FullName, sw, computername, d)

            'End If



        Next



    End Sub



    Protected Sub ExitThreadCore()

        exitWorkerSignal.Reset()

        MyBase.ExitThreadCore()

    End Sub

End Class

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33676135
You need a reference to the System.Windows.Forms library, and add an Imports System.Windows.Forms.
You also need to make sure the "Application type" is set to "Windows Forms Application", "Enable application framework" is off, and "Startup object" is "Sub Main"
0
 
LVL 22

Author Comment

by:yo_bee
ID: 33676138
also here is a Properties screenshot
Capture.JPG
0
 
LVL 22

Author Closing Comment

by:yo_bee
ID: 33676171
Thanks for all the help

This worked like a charm
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now