2

我有课 !

class Reg_v_no_string
{
    public static string key_place = "";
    public static string key = "";
    public static string value = "";
    public string reg_value(string key_place, string key)
    {
        string value = string.Empty;

            RegistryKey klase = Registry.CurrentUser;
            klase = klase.OpenSubKey(key_place);
            value = klase.GetValue(key).ToString();
            klase.Close();

        return value;
    }
}

这将返回空白消息框。怎么能解决这个问题!?谢谢 !

我怎样才能从这个类返回值?

我试过了

private void kryptonButton1_Click(object sender, EventArgs e)
    {
        Reg_v_no_string.key_place = @"Control Panel\Desktop";
        Reg_v_no_string.key_place = "WheelScrollLines";
        MessageBox.Show(Reg_v_no_string.value);

    }
4

3 回答 3

5

您需要实际调用该方法:

private void kryptonButton1_Click(object sender, EventArgs e) 
{ 
    var value = reg_value(@"Control Panel\Desktop", "WheelScrollLines");
    MessageBox.Show(value); 
} 

还要考虑对您的班级进行一些更改:

public static class Reg_v_no_string 
{ 
    public static string reg_value(string key_place, string key) 
    { 
        string value = string.Empty; 

         RegistryKey klase = Registry.CurrentUser; 
         // todo: add some error checking to make sure the key is opened, etc.
         klase = klase.OpenSubKey(key_place); 
         value = klase.GetValue(key).ToString(); 
         klase.Close(); 

        return value; 
    } 
} 

然后当你调用这个static类时,它是这样的:

// you don't need to `new` this class if it is static:
var value = Reg_v_no_string.reg_value(@"Control Panel\Desktop", "WheelScrollLines");

或者,保持它不是静态的:

public class Reg_v_no_string 
{ 
    public string reg_value(string key_place, string key) 
    { 
        string value = string.Empty; 

         RegistryKey klase = Registry.CurrentUser; 
         // todo: add some error checking to make sure the key is opened, etc.
         klase = klase.OpenSubKey(key_place); 
         value = klase.GetValue(key).ToString(); 
         klase.Close(); 

        return value; 
    } 
} 

然后像这样调用它:

Reg_v_no_string obj = new Reg_v_no_string ();
var value = reg_value(@"Control Panel\Desktop", "WheelScrollLines");
于 2012-07-23T22:58:01.230 回答
4

没有调用reg_value.

结果,您将获得value字段的默认值(也在实例之间共享)。

于 2012-07-23T22:54:25.943 回答
0

Reg_v_no_string.value尚未设置。
您必须调用reg_value(string key_place, string key)将返回该值的函数。

于 2017-05-16T06:30:48.697 回答