Passing username and Password to a batch file

Posted on 2010-09-18
Last Modified: 2012-06-27
I have a windows form in C# that I would like to use to pass the username and password to a batch file. Does anyone know how to do this?
the batch file code is
@echo off
pushd "%programfiles%\checkpoint\securemote\bin"
if not exist scc.exe goto scc_missing
echo #### Setting command-line mode
scc.exe setmode cli
echo #### Setting login information
echo #### Starting VPN connection
scc.exe connect -p Home
echo -
echo #### Testing connection
ping -w 1000 -n 4> NUL
if errorlevel 1 echo Connection test failed!
if errorlevel 1 goto disconnect
if errorlevel 0 echo Connection tested OK!
echo -
echo #### Disconnecting..
scc.exe disconnect -p
echo #### Erasing login information
scc.exe erasecreds
echo #### Setting GUI (connect) mode
scc.exe setmode con
goto end
echo #### SCC.EXE appears to be missing! Aborting..
goto end
echo #### Done!

Open in new window

The form code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace VPNTest
    static class Program
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
            Application.Run(new Form1());
            ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();

            //specify the name and the arguements you want to pass to the command prompt
            psi.FileName = @"c:\Program Files\Nomad\VPN.bat";
            psi.Arguments = "'+ txtBxUser +'", "'+ txtBxPass +'";

            //if you don't want a console window popping up then set this property
            pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            //Create new process and set the starting information
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo = psi;

            //Set this so that you can tell when the process has completed
            p.EnableRaisingEvents = true;
            //wait until the process has completed
            while (!p.HasExited)
            //check to see what the exit code was
            if (p.ExitCode != 0)
                //some error occurred

Open in new window

Question by:r3nder
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 65

Expert Comment

ID: 33708530
In your Process object p there is a property within StartInfo called Arguments, see if you can make use of that

LVL 65

Expert Comment

ID: 33708536
I think multiple arguments are delimited by a space so build a string with your username and password delimited by a space

in dos I think its %1 for first argument, %2 for second argument

Author Comment

ID: 33709070
I tried those suggestions but that didnt work - I have been searching the net for an example but nothing yet
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

LVL 65

Expert Comment

ID: 33709189
maybe you need to run the cmd.exe and use your batch script as args

c:\\windows\\system32\\cmd.exe  should be the program to run

arguments are  /c  batchscriptname  username  password

/c means run command and exit
u can use /k to test, this runs and leaves command window open - useful for determing whats happening
LVL 65

Accepted Solution

rockiroads earned 500 total points
ID: 33709199
ok, just gave it a test and is working fine this way
this is my test code

            string userName = "fred";
            String password = "flintstone";
            string args = "/k c:\\temp\\csharp.bat " + userName + " " + password;
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.Arguments = args;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "c:\\windows\\system32\\cmd.exe";

remember to change /k to /c

Author Comment

ID: 33709271
That did it - thanks man - I have never had to pass parameters to a batch file - I appreciate your knowledge :)

Author Closing Comment

ID: 33709274
Excellent to have had the privilege to learn from you
LVL 65

Expert Comment

ID: 33709321
No probs and thanks. It didn't click initially that it was a batch file. Only when I started thinking about the test that it dawned one me lol.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

617 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