C# 从注册表获取从字符串转换为int的int

C# 从注册表获取从字符串转换为int的int,c#,winforms,C#,Winforms,我有一个程序,它必须记住上次关闭时用户的位置。我选择在注册表中尝试此操作,但我不知道如何将从注册表中获取的值转换为int,以便将其设置为表单的位置。 以下是我的建议: ModifyRegistry myRegistry = new ModifyRegistry(); (< this is a field variable) private void RobCsvKopieren_Load(object sender, EventArgs e) {

我有一个程序,它必须记住上次关闭时用户的位置。我选择在注册表中尝试此操作,但我不知道如何将从
注册表中获取的值转换为int,以便将其设置为表单的位置。
以下是我的建议:

    ModifyRegistry myRegistry = new ModifyRegistry(); (< this is a field variable)


    private void RobCsvKopieren_Load(object sender, EventArgs e)
    {
        Location.X = ((int)myRegistry.Read("Location.X"));
    }

    private void RobCsvKopieren_LocationChanged(object sender, EventArgs e)
    {

    }

    private void RobCsvKopieren_FormClosing(object sender, FormClosingEventArgs e)
    {
        myRegistry.Write("Location.X", Location.X);
        myRegistry.Write("Location.Y", Location.Y);
        myRegistry.Write("Size", Size);
    }
ModifyRegistry myRegistry=new ModifyRegistry();(<这是一个字段变量)
私有void RobCsvKopieren_加载(对象发送方,事件参数e)
{
Location.X=((int)myRegistry.Read(“Location.X”);
}
私有无效RobCsvKopieren_位置已更改(对象发送方,事件参数e)
{
}
私有无效RobCsvKopieren_FormClosing(对象发送方,FormClosingEventArgs e)
{
写入(“Location.X”,Location.X);
myRegistry.Write(“Location.Y”,Location.Y);
写入(“大小”,大小);
}
PS:我对注册表的经验绝对是0,注册表修改的代码来自我在这个网站上多次找到的网站

PPS。这是Modify Registry类的代码(您可能会注意到,我根本没有更改它,因为我害怕破坏它。)

/* *************************************** 
 *           ModifyRegistry.cs
 * ---------------------------------------
 *         a very simple class 
 *    to read, write, delete and count
 *       registry values with C#
 * ---------------------------------------
 *      if you improve this code 
 *   please email me your improvement!
 * ---------------------------------------
 *         by Francesco Natali
 *        - fn.varie@libero.it -
 * ***************************************/

using System;
// it's required for reading/writing into the registry:
using Microsoft.Win32;
// and for the MessageBox function:
using System.Windows.Forms;

namespace Utility.ModifyRegistry
{
/// <summary>
/// An useful class to read/write/delete/count registry keys
/// </summary>
public class ModifyRegistry
{
    private bool showError = false;
    /// <summary>
    /// A property to show or hide error messages 
    /// (default = false)
    /// </summary>
    public bool ShowError
    {
        get { return showError; }
        set { showError = value; }
    }

    private string subKey = "SOFTWARE\\" + Application.ProductName.ToUpper();
    /// <summary>
    /// A property to set the SubKey value
    /// (default = "SOFTWARE\\" + Application.ProductName.ToUpper())
    /// </summary>
    public string SubKey
    {
        get { return subKey; }
        set { subKey = value; }
    }

    private RegistryKey baseRegistryKey = Registry.LocalMachine;
    /// <summary>
    /// A property to set the BaseRegistryKey value.
    /// (default = Registry.LocalMachine)
    /// </summary>
    public RegistryKey BaseRegistryKey
    {
        get { return baseRegistryKey; }
        set { baseRegistryKey = value; }
    }

    /* **************************************************************************
     * **************************************************************************/

    /// <summary>
    /// To read a registry key.
    /// input: KeyName (string)
    /// output: value (string) 
    /// </summary>
    public string Read(string KeyName)
    {
        // Opening the registry key
        RegistryKey rk = baseRegistryKey;
        // Open a subKey as read-only
        RegistryKey sk1 = rk.OpenSubKey(subKey);
        // If the RegistrySubKey doesn't exist -> (null)
        if (sk1 == null)
        {
            return null;
        }
        else
        {
            try
            {
                // If the RegistryKey exists I get its value
                // or null is returned.
                return (string)sk1.GetValue(KeyName.ToUpper());
            }
            catch (Exception e)
            {
                // AAAAAAAAAAARGH, an error!
                ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
                return null;
            }
        }
    }

    /* **************************************************************************
     * **************************************************************************/

    /// <summary>
    /// To write into a registry key.
    /// input: KeyName (string) , Value (object)
    /// output: true or false 
    /// </summary>
    public bool Write(string KeyName, object Value)
    {
        try
        {
            // Setting
            RegistryKey rk = baseRegistryKey;
            // I have to use CreateSubKey 
            // (create or open it if already exits), 
            // 'cause OpenSubKey open a subKey as read-only
            RegistryKey sk1 = rk.CreateSubKey(subKey);
            // Save the value
            sk1.SetValue(KeyName.ToUpper(), Value);

            return true;
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
            return false;
        }
    }

    /* **************************************************************************
     * **************************************************************************/

    /// <summary>
    /// To delete a registry key.
    /// input: KeyName (string)
    /// output: true or false 
    /// </summary>
    public bool DeleteKey(string KeyName)
    {
        try
        {
            // Setting
            RegistryKey rk = baseRegistryKey;
            RegistryKey sk1 = rk.CreateSubKey(subKey);
            // If the RegistrySubKey doesn't exists -> (true)
            if (sk1 == null)
                return true;
            else
                sk1.DeleteValue(KeyName);

            return true;
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, "Deleting SubKey " + subKey);
            return false;
        }
    }

    /* **************************************************************************
     * **************************************************************************/

    /// <summary>
    /// To delete a sub key and any child.
    /// input: void
    /// output: true or false 
    /// </summary>
    public bool DeleteSubKeyTree()
    {
        try
        {
            // Setting
            RegistryKey rk = baseRegistryKey;
            RegistryKey sk1 = rk.OpenSubKey(subKey);
            // If the RegistryKey exists, I delete it
            if (sk1 != null)
                rk.DeleteSubKeyTree(subKey);

            return true;
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, "Deleting SubKey " + subKey);
            return false;
        }
    }

    /* **************************************************************************
     * **************************************************************************/

    /// <summary>
    /// Retrive the count of subkeys at the current key.
    /// input: void
    /// output: number of subkeys
    /// </summary>
    public int SubKeyCount()
    {
        try
        {
            // Setting
            RegistryKey rk = baseRegistryKey;
            RegistryKey sk1 = rk.OpenSubKey(subKey);
            // If the RegistryKey exists...
            if (sk1 != null)
                return sk1.SubKeyCount;
            else
                return 0;
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, "Retriving subkeys of " + subKey);
            return 0;
        }
    }

    /* **************************************************************************
     * **************************************************************************/

    /// <summary>
    /// Retrive the count of values in the key.
    /// input: void
    /// output: number of keys
    /// </summary>
    public int ValueCount()
    {
        try
        {
            // Setting
            RegistryKey rk = baseRegistryKey;
            RegistryKey sk1 = rk.OpenSubKey(subKey);
            // If the RegistryKey exists...
            if (sk1 != null)
                return sk1.ValueCount;
            else
                return 0;
        }
        catch (Exception e)
        {
            // AAAAAAAAAAARGH, an error!
            ShowErrorMessage(e, "Retriving keys of " + subKey);
            return 0;
        }
    }

    /* **************************************************************************
     * **************************************************************************/

    private void ShowErrorMessage(Exception e, string Title)
    {
        if (showError == true)
            MessageBox.Show(e.Message,
                            Title
                            , MessageBoxButtons.OK
                            , MessageBoxIcon.Error);
    }
}
}
/******************************************************
*ModifyRegistry.cs
* ---------------------------------------
*一个非常简单的类
*读、写、删除和计数
*注册表值与C#
* ---------------------------------------
*如果你改进这个代码
*请电邮给我你的改进!
* ---------------------------------------
*弗朗西斯科·纳塔利
*-fn。varie@libero.it -
* ***************************************/
使用制度;
//读取/写入注册表时需要:
使用Microsoft.Win32;
//对于MessageBox功能:
使用System.Windows.Forms;
namespace Utility.ModifyRegistry
{
/// 
///用于读取/写入/删除/计数注册表项的有用类
/// 
公共类修改注册表
{
private bool-showError=假;
/// 
///显示或隐藏错误消息的属性
///(默认值=false)
/// 
公共浴室
{
获取{return showError;}
设置{showError=value;}
}
私有字符串subKey=“SOFTWARE\\”+Application.ProductName.ToUpper();
/// 
///用于设置子键值的属性
///(default=“SOFTWARE\\”+Application.ProductName.ToUpper())
/// 
公共字符串子密钥
{
获取{返回子键;}
设置{subKey=value;}
}
private RegistryKey baseRegistryKey=Registry.LocalMachine;
/// 
///用于设置BaseRegistryKey值的属性。
///(默认值=Registry.LocalMachine)
/// 
公共注册表项BaseRegistryKey
{
获取{return baseRegistryKey;}
设置{baseRegistryKey=value;}
}
/* **************************************************************************
* **************************************************************************/
/// 
///读取注册表项。
///输入:键名(字符串)
///输出:值(字符串)
/// 
公共字符串读取(字符串键名)
{
//打开注册表项
RegistryKey rk=baseRegistryKey;
//以只读方式打开子项
RegistryKey sk1=rk.OpenSubKey(subKey);
//如果注册表子项不存在->(null)
if(sk1==null)
{
返回null;
}
其他的
{
尝试
{
//如果RegistryKey存在,则获取其值
//或返回null。
返回(字符串)sk1.GetValue(KeyName.ToUpper());
}
捕获(例外e)
{
//aaaaaaaaaaaargh,一个错误!
消息(e,“读取注册表”+KeyName.ToUpper());
返回null;
}
}
}
/* **************************************************************************
* **************************************************************************/
/// 
///写入注册表项。
///输入:键名(字符串)、值(对象)
///输出:正确或错误
/// 
public bool Write(字符串键名、对象值)
{
尝试
{
//背景
RegistryKey rk=baseRegistryKey;
//我必须使用CreateSubKey
//(如果已退出,则创建或打开它),
//'因为OpenSubKey以只读方式打开子键
RegistryKey sk1=rk.CreateSubKey(子键);
//保存值
sk1.SetValue(KeyName.ToUpper(),Value);
返回true;
}
捕获(例外e)
{
//aaaaaaaaaaaargh,一个错误!
消息(e,“写入注册表”+KeyName.ToUpper());
返回false;
}
}
/* **************************************************************************
* **************************************************************************/
/// 
///删除注册表项。
///输入:键名(字符串)
///输出:正确或错误
/// 
public bool DeleteKey(字符串KeyName)
{
尝试
{
//背景
RegistryKey rk=baseRegistryKey;
RegistryKey sk1=rk.CreateSubKey(子键);
//如果注册表子项不存在->(true)
if(sk1==null)
返回true;
其他的
sk1.DeleteValue(键名);
返回true;
}
捕获(例外e)
{
//aaaaaaaaaaaargh,一个错误!
消息(e,“删除子键”+子键);
返回false;
}
}
/* **************************************************************************
* **************************************************************************/
/// 
///删除子密钥和任何
Location.X = Convert.ToInt32(myRegistry.Read("Location.X"));
int locationX;
var success = Int32.TryParse(myRegistry.Read("Location.X"), out locationX);
if(success)
 Location.X = locationX;
else
 Location.X = // your DefaultValue
    private void RobCsvKopieren_FormClosing(object sender, FormClosingEventArgs e)
    {
        Properties.Settings.Default.f_sOudeLocatie = f_sOudeLocatie;
        Properties.Settings.Default.f_sNieuweLocatie = f_sNieuweLocatie;
        Properties.Settings.Default.LastFormLocation = this.Location;
        Properties.Settings.Default.LastFormSize = this.Size;
        myRegistry.Write("Size", Size);
        Properties.Settings.Default.Save();
    }
    private void RobCsvKopieren_Load(object sender, EventArgs e)
    {
        if (Properties.Settings.Default.f_sOudeLocatie != "")
        {
            fdlg.InitialDirectory = f_sOudeLocatie;
        }else
        {
            fdlg.InitialDirectory = Properties.Settings.Default.f_sOudeLocatie;
        }
        this.Location = Properties.Settings.Default.LastFormLocation;
        this.Size = Properties.Settings.Default.LastFormSize;
    }