• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Passing a variable

HI,
Im trying to pass the variable “hostname_value” into the stfamtv0001 string and replace the \\\\28.228.176.111.  The hostname_value variable is taken from the event logs on a machine.  Just not sure how to code it so its passed correctly into the string.

Help please!

        string stfamtv0001 = "-i 1 -d -u Administrator -p Administrator \\\\28.228.176.111 notepad.exe";


        private void button1_Click(object sender, EventArgs e)
        {
            PCdata(stfamtv0001);
        }

        private void PCdata(string PCstreamPath)
        {
            System.Diagnostics.Process ps;
            ps = new System.Diagnostics.Process();
            ps.EnableRaisingEvents = false;
            ps.StartInfo.UseShellExecute = false;
            System.Diagnostics.Process.Start("psexec.exe", PCstreamPath);
            ps.Close();
        }

        String hostname_value = "";

        private void data(string streamPath)
        {
            System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables();

            foreach (System.Collections.DictionaryEntry envVar in envVars)
            {

                if (envVar.Key.ToString() == "ThinConnectedFrom")
                {
                    hostname_value = envVar.Value.ToString();
                }
            }
        }
0
PeterSchenk
Asked:
PeterSchenk
  • 13
  • 13
1 Solution
 
käµfm³d 👽Commented:
How about:
string stfamtv0001 = string.Format("-i 1 -d -u Administrator -p Administrator {0} notepad.exe", hostname_value);

Open in new window

0
 
PeterSchenkAuthor Commented:
Get the following compile error

Error      4      A field initializer cannot reference the non-static field, method, or property 'WindowsFormsApplication6.Form1.hostname_value'      c:\documents and settings\schenkp\my documents\visual studio 2010\Projects\WindowsFormsApplication6\WindowsFormsApplication6\Form1.cs      29      105      WindowsFormsApplication6
0
 
käµfm³d 👽Commented:
I did not look close enough at your code, it seems   : \

How about we change it to this:

stfamtv0001 = string.Format("-i 1 -d -u Administrator -p Administrator {0} notepad.exe", hostname_value);

Open in new window


that is, take off the type name from the beginning, and add it just after this line:

hostname_value = envVar.Value.ToString();

Open in new window

0
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.

 
PeterSchenkAuthor Commented:
Ok changed the code to this.... and got the errors in the attached file.

    stfamtv0001 = string.Format("-i 1 -d -u Administrator -p Administrator {0} notepad.exe", hostname_value);
       hostname_value = envVar.Value.ToString();


        private void button1_Click(object sender, EventArgs e)
        {
            PCdata(stfamtv0001);
        }

        private void PCdata(string PCstreamPath)
        {
            System.Diagnostics.Process ps;
            ps = new System.Diagnostics.Process();
            ps.EnableRaisingEvents = false;
            ps.StartInfo.UseShellExecute = false;
            System.Diagnostics.Process.Start("psexec", PCstreamPath);
            ps.Close();
        }

       String hostname_value = "";

        private void data(string streamPath)
        {
            System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables();

            foreach (System.Collections.DictionaryEntry envVar in envVars)
            {

                if (envVar.Key.ToString() == "ThinConnectedFrom")
                {
                    hostname_value = envVar.Value.ToString();
                }
            }
        }

 Errors
0
 
käµfm³d 👽Commented:
That's backwards. What I was referring to was this (paraphrased):

string stfamtv0001;

...

private void data(string streamPath)
{
    System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables();

    foreach (System.Collections.DictionaryEntry envVar in envVars)
    {

        if (envVar.Key.ToString() == "ThinConnectedFrom")
        {
            hostname_value = envVar.Value.ToString();
            stfamtv0001 = string.Format("-i 1 -d -u Administrator -p Administrator {0} notepad.exe", hostname_value);  // <<====
        }
    }
}

Open in new window

0
 
PeterSchenkAuthor Commented:
lol...

Now complies correctly but does not pass the first part of the string correctly.... so PSEXEC starts but none of the command line arguments are beings passed like these


"-i 1 -d -u Administrator -p Administrator {0} notepad.exe"

Screen flashes to fast to be sure....  anyway of captureing the data?
0
 
PeterSchenkAuthor Commented:
After debugging it the issue is that no data from stfamtv0001 string is being passed to the event click…  so the method below is not being used at all.

       String hostname_value = "";

       private void data(string streamPath)
       {
           System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables();

           foreach (System.Collections.DictionaryEntry envVar in envVars)
           {

               if (envVar.Key.ToString() == "ThinConnectedFrom")
               {
                   hostname_value = envVar.Value.ToString();
                   stfamtv0001 = string.Format("-i 1 -d -u Administrator -p Administrator {0} notepad.exe", hostname_value);  // <<====
                  // stfamtv0001 = string.Format("-i 1 -d -u Administrator -p Administrator \\\\28.228.176.111 vlc.exe --video-on-top --volume 512 rtsp://stfamtv0001/streama");
               }
           }
       }
0
 
käµfm³d 👽Commented:
You will have to call your data function at some point before you call the function PCdata.
0
 
PeterSchenkAuthor Commented:
ok how do we do that?
0
 
käµfm³d 👽Commented:
Well you *could* do it this way:

private void button1_Click(object sender, EventArgs e)
{
    data(null);
    PCdata(stfamtv0001);
}

Open in new window


...but that means that the variable will be set every time you click the button. This may or may not be what you want. It may not be of consequence if you do run it every time.

P.S.

I passed null to the function because it doesn't look like you are even using the variable streamPath in your data function. You can change what you are passing if you need to (i.e. don't think you have to pass null).
0
 
PeterSchenkAuthor Commented:
Did not change anything its simply not passing anything out of the Data method.
0
 
käµfm³d 👽Commented:
OK. I took your original posting to imply that you stfamtv0001 outside of any function--meaning that you declared it at the class level. Perhaps I am misinterpreting your code. Is it possible to post the entire class?
0
 
PeterSchenkAuthor Commented:
There is no class :) this is all the code below :)

string stfamtv0001 = "-i 1 -d -u Administrator -p Administrator \\\\28.228.176.111 notepad.exe";


        private void button1_Click(object sender, EventArgs e)
        {
            PCdata(stfamtv0001);
        }

        private void PCdata(string PCstreamPath)
        {
            System.Diagnostics.Process ps;
            ps = new System.Diagnostics.Process();
            ps.EnableRaisingEvents = false;
            ps.StartInfo.UseShellExecute = false;
            System.Diagnostics.Process.Start("psexec.exe", PCstreamPath);
            ps.Close();
        }

        String hostname_value = "";

        private void data(string streamPath)
        {
            System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables();

            foreach (System.Collections.DictionaryEntry envVar in envVars)
            {

                if (envVar.Key.ToString() == "ThinConnectedFrom")
                {
                    hostname_value = envVar.Value.ToString();
                }
            }
        }
0
 
käµfm³d 👽Commented:
There is no class
Whoa! I just had a Matrix flashback...

There has to be a class!! Since you appear to be working with a Forms project, you should have something like:

public class Form1  // <<--- Name could be different
{
string stfamtv0001 = "-i 1 -d -u Administrator -p Administrator \\\\28.228.176.111 notepad.exe";


        private void button1_Click(object sender, EventArgs e)
        {
            PCdata(stfamtv0001);
        }

        private void PCdata(string PCstreamPath)
        {
            System.Diagnostics.Process ps;
            ps = new System.Diagnostics.Process();
            ps.EnableRaisingEvents = false;
            ps.StartInfo.UseShellExecute = false;
            System.Diagnostics.Process.Start("psexec.exe", PCstreamPath);
            ps.Close();
        }

        String hostname_value = "";

        private void data(string streamPath)
        {
            System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables();

            foreach (System.Collections.DictionaryEntry envVar in envVars)
            {

                if (envVar.Key.ToString() == "ThinConnectedFrom")
                {
                    hostname_value = envVar.Value.ToString();
                }
            }
        } 
}

Open in new window

0
 
PeterSchenkAuthor Commented:
Ok so now lets assume that I want to have more variables like stfamtv0002, stfamtv0003 etc... what would the final code look like.  Sorry im not a propramer by nature and in way over my head on this project... lol

You are now in the MATRIX :)
0
 
käµfm³d 👽Commented:
Assuming your class is named Form1 (you can change the name in the following if it is different), try this:

using System;
using System.Windows.Forms;

namespace _27413983   // Change this to match what is in Form1.Designer.cs
{
    public partial class Form1 : Form  // Change this to match what is in Form1.Designer.cs
    {
        private string stfamtv0001 = "-i 1 -d -u Administrator -p Administrator {0} notepad.exe";
        private string stfamtv0002; // New variable
        private string stfamtv0003; // New variable
        private string hostname_value;

        public Form1()
        {
            InitializeComponent();
            data();  // Now the hostname_value variable is only set once
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(hostname_value))
            {
                MessageBox.Show("Hostname was not set!");
            }
            else
            {
                PCdata(stfamtv0001);
            }
        }

        private void PCdata(string PCstreamPath)
        {
            System.Diagnostics.Process ps;
            ps = new System.Diagnostics.Process();
            ps.EnableRaisingEvents = false;
            ps.StartInfo.UseShellExecute = false;
            System.Diagnostics.Process.Start("psexec.exe", PCstreamPath);
            ps.Close();
        }

        private void data()
        {
            System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables();

            foreach (System.Collections.DictionaryEntry envVar in envVars)
            {
                if (envVar.Key.ToString() == "ThinConnectedFrom")
                {
                    hostname_value = string.Format(stfamtv0001, envVar.Value.ToString());
                }
            }
        }
    }
}

Open in new window

0
 
PeterSchenkAuthor Commented:
Keep poping up with the message box.... still seems like the Data method is not passing anything... like it.  You can test this too my creating an evniorment entry called :

ThinConnectedFrom=some random ip address

then see if it passes it along....

Thanks for sticking with me on this one :)
0
 
käµfm³d 👽Commented:
This is most likely because your call to GetEnvironmentVariables isn't returning the new variable--at least it didn't in my test. I ended up having to do:

System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine);

Open in new window


Prior to trying this change, you can confirm by putting a breakpoint on the line

if (envVar.Key.ToString() == "ThinConnectedFrom")

Open in new window


when the breakpoint hits, mouse over the envVars variable and you should be able to see what is in the collection. For example:

Screenshot
You can click in the margin where you see the red dot/yellow arrow in my screenshot to set the breakpoint.
0
 
PeterSchenkAuthor Commented:
OK so couple of questions....

1)  If the user click on a button 2 lets say and wants to use the string stfamtv0002, how do we change the code....

2)  what should the final code look like?
0
 
käµfm³d 👽Commented:
For that, I think I led you slightly astray. It would be better to call your data method multiple times. I have reworked the code to account for that:

using System;
using System.Windows.Forms;

namespace _27413983   // Change this to match what is in Form1.Designer.cs
{
    public partial class Form1 : Form  // Change this to match what is in Form1.Designer.cs
    {
        private string stfamtv0001 = "-i 1 -d -u Administrator -p Administrator {0} notepad.exe";
        private string stfamtv0002; // New variable
        private string stfamtv0003; // New variable
        private string hostname_value;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            data(stfamtv0001);

            if (string.IsNullOrEmpty(hostname_value))
            {
                MessageBox.Show("Hostname was not set!");
            }
            else
            {
                PCdata(hostname_value);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            data(stfamtv0002);

            if (string.IsNullOrEmpty(hostname_value))
            {
                MessageBox.Show("Hostname was not set!");
            }
            else
            {
                PCdata(hostname_value);
            }
        }

        private void PCdata(string PCstreamPath)
        {
            System.Diagnostics.Process ps;
            ps = new System.Diagnostics.Process();
            ps.EnableRaisingEvents = false;
            ps.StartInfo.UseShellExecute = false;
            System.Diagnostics.Process.Start("psexec.exe", PCstreamPath);
            ps.Close();
        }

        private void data(string streamPath)
        {
            System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables();

            foreach (System.Collections.DictionaryEntry envVar in envVars)
            {
                if (envVar.Key.ToString() == "ThinConnectedFrom")
                {
                    hostname_value = string.Format(streamPath, envVar.Value.ToString());
                }
            }
        }
    }
}

Open in new window

0
 
PeterSchenkAuthor Commented:
thats strange its still not passing up the hostname_value

I also put back in this line

System.Collections.IDictionary envVars = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine);
0
 
käµfm³d 👽Commented:
Do you see your environment variable in envVars if you set a breakpoint and mouse over it?
0
 
PeterSchenkAuthor Commented:
yes it has a count of 60.... my not ThinConnectFrom Ip address....
0
 
käµfm³d 👽Commented:
???

Did you mean you do or do not see ThinConnectFrom?
0
 
PeterSchenkAuthor Commented:
Got it to work by change it from a machine variable to a user variable : )

Thanks for you awesome support .....
0
 
käµfm³d 👽Commented:
Got it to work by change it from a machine variable to a user variable
Cool. The difference between our results is due to where I added the environment variable (which I apparently did at the machine level!).

Glad to help  = )
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 13
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now