?
Solved

Passing a variable

Posted on 2011-10-25
26
Medium Priority
?
235 Views
Last Modified: 2012-05-12
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
Comment
Question by:PeterSchenk
  • 13
  • 13
26 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37024117
How about:
string stfamtv0001 = string.Format("-i 1 -d -u Administrator -p Administrator {0} notepad.exe", hostname_value);

Open in new window

0
 

Author Comment

by:PeterSchenk
ID: 37024133
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37024182
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:PeterSchenk
ID: 37024287
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37024303
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
 

Author Comment

by:PeterSchenk
ID: 37024396
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
 

Author Comment

by:PeterSchenk
ID: 37024477
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37024560
You will have to call your data function at some point before you call the function PCdata.
0
 

Author Comment

by:PeterSchenk
ID: 37024651
ok how do we do that?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37024689
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
 

Author Comment

by:PeterSchenk
ID: 37024769
Did not change anything its simply not passing anything out of the Data method.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37024830
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
 

Author Comment

by:PeterSchenk
ID: 37025051
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37025073
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
 

Author Comment

by:PeterSchenk
ID: 37025154
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37025279
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
 

Author Comment

by:PeterSchenk
ID: 37025594
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37026030
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
 

Author Comment

by:PeterSchenk
ID: 37026074
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37026119
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
 

Author Comment

by:PeterSchenk
ID: 37026339
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37026384
Do you see your environment variable in envVars if you set a breakpoint and mouse over it?
0
 

Author Comment

by:PeterSchenk
ID: 37026402
yes it has a count of 60.... my not ThinConnectFrom Ip address....
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37026567
???

Did you mean you do or do not see ThinConnectFrom?
0
 

Author Comment

by:PeterSchenk
ID: 37026661
Got it to work by change it from a machine variable to a user variable : )

Thanks for you awesome support .....
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37026687
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 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