使用注册表项填充文本框 [英] populating a text box with registry key
问题描述
好的,所以我一直在做一些事情并且有很好的工作,但是我对下一步感到难过。我是新手,所以学习很有趣,但是当你到达终点时也会感到沮丧。
Ok so I have been working on something and got a good part working but I'm stumped on this next step. I am new to this so it is fun learning but also frustrating when you hit the end of the line.
使用.Net 2.0
working with .Net 2.0
什么我正在检查是否存在注册表项。如果没有,创建它 - 这个工作
What I am doing is checking if a registry key exists. If it doesn't, create it - this works
找到一个字符串值,如果它不存在,创建它并填充主机名(也填写窗体文本框) - 作品
Find a string value and if it doesn't exist, create it and populate with hostname (also fill out the windows form textbox) - works
我想不出的是下一部分。如果 关闭我的窗体,再次运行,是的主机名在那里;但是,如果我然后删除该值(所以键仍然存在),它只是填充空白。
What I cannot figure out is the next part. If close my windows form, run it again, yes the hostname is there; however, if I then delete the value (so the key still exists) it just populates blank.
我希望它看到FriendName为空并将主机名放在那里...任何帮助非常感谢。我的日子结束了,所以希望有时间可以帮助别人。
I want it to see FriendName is blank and put the hostname in there... any help greatly appreciated. My day is ending so hopefully some time for someone to assist.
我也把完整的代码放在这个下面......
I have put the FULL code below this too as well...
private void Form1_Load(object sender, EventArgs e)
{
var pa = (System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));
if (pa == "x86")
{ strSubKeyPath = "SOFTWARE"; }
else
{ strSubKeyPath = "SOFTWARE\\Wow6432Node"; }
RegistryKey pRegKey = Registry.LocalMachine.OpenSubKey(strSubKeyPath, true);
pRegKey.CreateSubKey("BMSLCS");
pRegKey = pRegKey.OpenSubKey("BMSLCS", true);
Object myFriendlyName = pRegKey.GetValue("FriendName");
if (myFriendlyName == null) { pRegKey.SetValue("FriendName", System.Environment.MachineName); }
else { txtFriendlyName.Text = myFriendlyName.ToString(); }
using System;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Diagnostics;
namespace LCSInventoryLite
{
public partial class Form1 : Form
{
private string strSubKeyPath;
public Form1()
{ InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e)
{
var pa = (System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));
if (pa == "x86")
{ strSubKeyPath = "SOFTWARE"; }
else
{ strSubKeyPath = "SOFTWARE\\Wow6432Node"; }
RegistryKey pRegKey = Registry.LocalMachine.OpenSubKey(strSubKeyPath, true);
pRegKey.CreateSubKey("BMSLCS");
pRegKey = pRegKey.OpenSubKey("BMSLCS", true);
Object myFriendlyName = pRegKey.GetValue("FriendName");
if (myFriendlyName == null) { pRegKey.SetValue("FriendName", System.Environment.MachineName); }
else { txtFriendlyName.Text = myFriendlyName.ToString(); }
Object myOwner = pRegKey.GetValue("LAB-Client");
if (myOwner == null) { pRegKey.SetValue("LAB-Client", ""); }
else {txtOwner.Text = myOwner.ToString(); }
Object myLabLocation = pRegKey.GetValue("Lab-Location");
if (myLabLocation == null) { pRegKey.SetValue("Lab-Location", ""); }
else {txtLabLocation.Text = myLabLocation.ToString(); }
Object myDepartment = pRegKey.GetValue("ISSystemNetworked");
if (myDepartment == null) { pRegKey.SetValue("ISSystemNetworked", ""); }
else { cbDepartment.Text = myDepartment.ToString(); }
Object myNewFriendlyName = pRegKey.GetValue("FriendName");
txtFriendlyName.Text = myNewFriendlyName.ToString();
}
private void bOk_Click(object sender, EventArgs e)
{
var pa = (System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"));
if (pa == "x86")
{ strSubKeyPath = "SOFTWARE"; }
else
{ strSubKeyPath = "SOFTWARE\\Wow6432Node"; }
RegistryKey pRegKey = Registry.LocalMachine.OpenSubKey(strSubKeyPath, true);
pRegKey.CreateSubKey("BMSLCS");
pRegKey = pRegKey.OpenSubKey("BMSLCS", true);
pRegKey.SetValue("FriendName", txtFriendlyName.Text);
pRegKey.SetValue("LAB-Client", txtOwner.Text);
pRegKey.SetValue("Lab-Location", txtLabLocation.Text);
pRegKey.SetValue("ISSystemNetworked", cbDepartment.Text);
Process proc = null;
try
{
string batDir = string.Format(@"c:\btop\bginfo");
proc = new Process();
proc.StartInfo.WorkingDirectory = batDir;
proc.StartInfo.FileName = "refreshbginfo.cmd";
proc.StartInfo.CreateNoWindow = false;
proc.Start();
proc.WaitForExit();
}
catch
{
}
Application.Exit();
}
private void bCancel_Click_1(object sender, EventArgs e)
{ Application.Exit(); }
}
}
推荐答案
想通了......在我学习的过程中,我迷失了为什么如果我做了一个if而且,我不能做其他的如果...
figured it out... with me learning, I was lost why if I did an if and else, I couldn't do an Else If...
现在我看到了,你不需要这样做。继续做IF。然后我在网上发现了一个论坛主题并将其拉进来,现在我已经金了!
well now I see it, you don't need to do that. Just keep doing IF. I then found a forum topic on the web and pulled that in and now I'm golden!
if (myFriendlyName != null) { pRegKey.SetValue("FriendName", System.Environment.MachineName); }
这篇关于使用注册表项填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!