我有一个程序能够使用使用VS 2010编译和创建的C#代码检索各种注册表值。
但是,当我尝试将从Windows注册表中检索到的结果显示到表单中的富文本框时,会出现问题。
表单只显示1行,它是Array中包含结果的最后一个值。
请对代码提出一些建议。谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Syscrawl
{
public partial class FTK_Menu_Browsing_History : Form
{
public FTK_Menu_Browsing_History()
{
InitializeComponent();
}
private void buttonFileHistory_Click(object sender, EventArgs e)
{
this.Hide();
FTK_Menu_File_History mfh = new FTK_Menu_File_History();
mfh.ShowDialog();
this.Close();
}
private void buttonEncryptedFiles_Click(object sender, EventArgs e)
{
this.Hide();
FTK_Menu_Encrypted_Files mef = new FTK_Menu_Encrypted_Files();
mef.ShowDialog();
this.Close();
}
private void buttonRecentlyAccessedFiles_Click(object sender, EventArgs e)
{
this.Hide();
FTK_Menu_Recently_Accessed_Files mraf = new FTK_Menu_Recently_Accessed_Files();
mraf.ShowDialog();
this.Close();
}
private void buttonRegistryHistory_Click(object sender, EventArgs e)
{
this.Hide();
FTK_Menu_Registry_History mrh = new FTK_Menu_Registry_History();
mrh.ShowDialog();
this.Close();
}
private void buttonMainMenu_Click(object sender, EventArgs e)
{
this.Hide();
Menu m = new Menu();
m.ShowDialog();
this.Close();
}
private void buttonLogOut_Click(object sender, EventArgs e)
{
this.Hide();
Syscrawl_Login sl = new Syscrawl_Login();
sl.ShowDialog();
this.Close();
}
private void FTK_Menu_Browsing_History_Load(object sender, EventArgs e)
{
try
{
RegistryKey rk = Registry.CurrentUser;
rk = rk.OpenSubKey("Software\\Microsoft\\Internet Explorer\\TypedURLs",
false);
PrintKeys(rk);
rk.Close();
}
catch (Exception MyError)
{
richTextBoxBrowsing.Text="An error has occurred: " + MyError.Message;
}
}
void PrintKeys(RegistryKey rk)
{
if (rk == null)
{
richTextBoxBrowsing.Text="Couldn't open the desired subkey.";
return;
}
richTextBoxBrowsing.Text = "Subkeys of " + rk.Name;
try
{
string[] valnames = rk.GetValueNames();
int i = 0;
foreach (string s in valnames)
{
string val = (string)rk.GetValue(valnames[i++]);
richTextBoxBrowsing.Text="-----------------------------------------------";
richTextBoxBrowsing.Text=s + " contains " + val;
}
}
catch (Exception MyError)
{
richTextBoxBrowsing.Text = "An errors has occurred: " + MyError.Message;
}
}
private void richTextBoxBrowsing_TextChanged(object sender, EventArgs e)
{
}
}
}
答案 0 :(得分:4)
说:
richTextBoxBrowsing.Text=
在循环的每次迭代中,你继续覆盖文本。因此,只打印最后一次调用Text属性。
您需要将richTextBoxBrowsing.TextMode
属性设置为多行,然后调用:
richTextBoxBrowsing.AppendText(s + " contains " + val + "\n");
哦,顺便说一下,使用:string val = rk.GetValue(s).ToString();
因此您可以删除int i = 0;
声明
答案 1 :(得分:1)
richTextBoxBrowsing.TextMode
来
MultiLine
richTextBoxBrowsing.Text+=s +
" contains " + val;
Debug.WriteLine
调试返回
值答案 2 :(得分:1)
您应该使用richTextBoxBrowsing.AppendText(...)
答案 3 :(得分:1)
您正在使用每次调用更改整个内容,而不是附加到它:
richTextBoxBrowsing.Text=s + " contains " + val;
应该是
richTextBoxBrowsing.AppendText(s+" contains " + val+Environment.NewLine);
答案 4 :(得分:0)
我认为你的循环不正确。
在_void PrintKeys()_
你的循环过度使用RichTextBox.Text-Value
尝试类似:
String temp = "-----------------------------------------------";
foreach (string s in valnames)
{
String val = (String)rk.GetValue(valnames[i++]);
temp=temp+s + " contains " + val;
}
RichTextBox.text = temp;