enviroment variables in c#

My application "SC"  is going to exectued standalone on a desktop (the user will enter a username / passwrd to connect to the database) Also it connects via another application that is already logged onto the database.

The question I have is how can i pick up the enviroment variables related to user / password / and database when the application "SC" if executed from another application that is already connected to the oracle database. The other application would be executed from a citrix server if that makes any difference ...

I have tried to use

                test = Environment.GetEnvironmentVariable("USER");

                MessageBox.Show("test = " + test + " \n"
                     , "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

test is always blank wether its from the desktop or from the citrix application
Jayesh AcharyaTechnichal ConsultantAsked:
Who is Participating?
kris_perConnect With a Mentor Commented:

It must be 'USERNAME' (not USER).
kris_perConnect With a Mentor Commented:

This USERNAME will give the user name of the user currently logged into the computer....not the database username...if this is sql server and db login uses windows authentication...then USERNAME is the same for db login...

These are system defined enviornment vars (http://vlaurie.com/computers2/Articles/environment.htm)
Are you saving values in custom environment variabls and then trying to read from another app?

Also the environment where a local app runs and where a citrix app runs are totally different. You can consider it like citrix apps are running in another machine....so you cant really communicate between then using the system environment...but they can communicate using other shared resources like database or shared folder/file...

See if this helps....

Gururaj BadamConnect With a Mentor Commented:
when you say username/passwd, currently logged in user to the windows machine or to the DB?
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Jayesh AcharyaTechnichal ConsultantAuthor Commented:
the username /password is the oracle database, the application that executes "SC" is a windows gui app and the SC app is a WinFrom written in C#
kris_perConnect With a Mentor Commented:
You can check using the variable name 'USERNAME' (instead of USER) and see what it gives both in local and in citrix. I think it wont be empty and note that it is not oracle username.

Instead of using system environment variable to get the username, etc, you can pass them as command line argument to the SC app and in SC app, get them from command line args as shown in the code below:
In GUIApp - when starting sc app:
            string userName = "oracle user name used by the connection";
            string password = "oracle password used by the connection";

            Process scProcess = new Process();
            scProcess.StartInfo.FileName = "sc.exe";
            scProcess.StartInfo.Arguments = userName + " " + password;


In SC app:

            // In the place where you want to get username 
            // and password, get them like below:

            string[] arguments = Environment.GetCommandLineArgs();
            string userName = arguments[1];
            string password = arguments[2];


Open in new window

Gururaj BadamConnect With a Mentor Commented:
If i've understood correctly I don't think you will be able to get DB UserName/Passwd from the environment variables unless the caller is setting them explicitly.
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
thanks everyone for the help, it has helped me get to the information i need, in the end i used
what I found was that when the calling program called SC, it had looged onto the oracle database 1st, so the envirment variables where set for that instance, but when SC is called directly from windows you have not logged onto the database and those variableswhere not set

            test = Environment.GetCommandLineArgs();
            for (int i = 0; i < test.GetLength(0); i++)
                 if (test[i].StartsWith("USER="))
                 { luser = test[i].Substring(5); }
                 else if (test[i].StartsWith("PASSWORD="))
                 {  lpswd = test[i].Substring(9); }
                 else if (test[i].StartsWith("DATABASE="))
                 {  ldb = test[i].Substring(9); }
Jayesh AcharyaTechnichal ConsultantAuthor Commented:
gave good pointers and helped me understand what direction to take
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.