Jump to content

C# : Application Launcher (Console Application)

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

Application Launcher (Console Application)



Difficulty: 2 / 10
Assumed Knowledge: Basic methods and functions of Visual Basic.
Information: This tutorial teaches you the knowledge of launching application.

This little project allows you to luanch an application through your console.


Step 1: Setting the console up

To start this project, you will need to create a new visual C# console.


Posted Image

Step 2: Adding variables
Class:
namespace Launch_Sample__console_

{

    class Program

    {

        static void Main()

        {


        }

    }

}

Using statements:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


using System.Diagnostics;

Step 3: Starting the main process

Starting the main part of the console.


            String txtApp;


            Console.Write("Option: ");

            txtApp = Console.ReadLine();

Step 4: Try / Catch statement
            try

            {

                Process.Start(txtApp);

                Console.WriteLine("Application: " + txtApp + ", Sucessfully Launched!");

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }


            Console.Read();

If the input the user types is correct, the application that matches the users input will run, But if the input the users type is incorrect, and does not match an application it will not run.

A Process component provides access to a process that is running on a computer. A process, in the simplest terms, is a running application. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the code of the process, including parts currently being executed by another thread.


Finished Source Code:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;


// Jarryd Hoffman


namespace Launch_Sample__console_

{

    class Program

    {

        static void Main()

        {

            String txtApp;


            Console.Write("Option: ");

            txtApp = Console.ReadLine();


            try

            {

                Process.Start(txtApp);

                Console.WriteLine("Application: " + txtApp + ", Sucessfully Launched!");

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

            }


            Console.Read();

        }

    }

}


Success:
Posted Image

Fail:
Posted Image

Images uploaded with ImageShack.us



#2
ack909

ack909

    Newbie

  • Members
  • Pip
  • 3 posts
I like this.

#3
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
nice! I'd give a +rep for an explanation of StartInfo, and stdio redirection, which would turn this into a very cool tutorial :c-cool:

#4
legacy

legacy

    Newbie

  • Members
  • PipPip
  • 16 posts
hi, im brand new to programming in general and i tried this tutorial and have two errors, both saying "the name txtApp does not exist in the current context" can someone tell me what i did wrong and how to fix it?

#5
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
that means you haven't declared the String variable txtApp.

make certain you declare the variable before you try and access it

String txtApp;



#6
legacy

legacy

    Newbie

  • Members
  • PipPip
  • 16 posts
i even copied and pasted code and for some reason it doesn't work

#7
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts

legacy said:

i even copied and pasted code and for some reason it doesn't work

Hmm, thats strange, I agree with sam, make sure you have declared it correctly, if that does not work, I suggect starting a new console application and rewrite the whole thing again.

Thanks guys :D

#8
Overkill

Overkill

    Newbie

  • Members
  • PipPip
  • 10 posts
Awesome, a friend of mine made something similar but wouldn't tell me how he made it >.<, gonna tune it a bit to make it better than his >:D!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users