Jeffrey Riggs

Jeffrey Riggs

  • 0
  • 14
  • 613

Help with my first C# - using If statement

May 22 2017 6:48 PM
forgot to state this first. doing .net 2.0 as we do not have any .net higher on our win7 boxes, unfortunately.
 
Ok so I have never done any C# coding and thought I would play around as I have a need for it. What I do not know how to do is change a variable if a variable contains something.
example... on all Windows PCs, you can look at the registry:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
there is a string called PROCESSOR_ARCHITECTURE. It will contain x86 or AMD64. On my test machine I have the read of that key working but what I dont know how to do is setup another variable based on that.
 
reason for this is that I am trying to read in 4 regkeys and it needs to work on 32bit and 64bit machines and I this is the only way I know how to do it (as I use something similar in vbscript)
 
so my code.. very last line returns AMD64 - thus that part is working
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace LCSInventoryLite
{
public partial class Form1 : Form
{ private object myProc;
public Form1()
{ InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
RegistryKey pRegKey = Registry.LocalMachine;
pRegKey = pRegKey.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment");
myProc = pRegKey.GetValue("PROCESSOR_ARCHITECTURE");
Console.WriteLine("x86 or AMD64: {0}", myProc);
 
 
so from here I am trying to set an if statement where if it is
x86> SOFTWARE\BMSLCS
AMD64>  SOFTWARE\Wow6432Node\BMSLCS
 
I have tried so many iterations and threw in the Console.WriteLine and it comes up blank every time...  
 
if (myProc = "AMD64") {pRegKey = "SOFTWARE\Wow6432Node\BMSLCS"; }
 
if (myProc = "AMD64") {pRegKey = pRegKey.OpenSubKey(@"SOFTWARE\Wow6432Node\BMSLCS");}
 
 pRegKey is blank now
 
I need to read in from that key.. if I get rid of the checking x86 and AMD64 and just hard code in the 64bit path this next part works just fine...
so when i launch my form it goes in and reads these 4 keys and populates them into the form. 
 
Object myFriendlyName = pRegKey.GetValue("FriendName");
Object myOwner = pRegKey.GetValue("LAB-Client");
Object myLabLocation = pRegKey.GetValue("Lab-Location");
Object myDepartment = pRegKey.GetValue("ISSystemNetworked");
txtFriendlyName.Text = myFriendlyName.ToString();
txtOwner.Text = myOwner.ToString();
txtLabLocation.Text = myLabLocation.ToString();
cbDepartment.Text = myDepartment.ToString();
 

Answers (6)