-1

我有这个程序,它必须记住用户上次关闭它的位置。我选择使用注册表尝试此操作,但我不知道如何将从中获取的值转换为registry.readint,以便我可以将其设置为表单的位置。这就是我所拥有的:

    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);
    }

PS:我对注册表的经验绝对是0,注册表修改的代码来自我在这个网站上多次找到的一个网站。

聚苯乙烯。这是修改注册表类的代码(您可能会注意到我根本没有更改它,因为我害怕破坏它。:

/* *************************************** 
 *           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);
    }
}
}
4

2 回答 2

3

这里的问题只是转换为 Int 吗?如果是,您应该使用:

Location.X = Convert.ToInt32(myRegistry.Read("Location.X"));

注意:如果它不是一个 int 值,你会得到一个 FormatException 来避免它,你可以使用:

int locationX;
var success = Int32.TryParse(myRegistry.Read("Location.X"), out locationX);
if(success)
 Location.X = locationX;
else
 Location.X = // your DefaultValue
于 2013-09-04T10:26:02.747 回答
1

在 Paulie Waulie 的帮助下,它通过以下评论修复:

“而不是使用注册表,您可以不使用 .NET 用户设置吗?您可以使用自定义类型,以便您可以将指标存储在自定义类型中并将其存储在那里:msdn.microsoft.com/en-us/library/bb397750。 aspx"

我为它创建了设置。并通过填写 FormClosing 事件的设置来完成它。

    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();
    }

并将他们指定到 this.location 上

    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;
    }

特别感谢 Paulie Waulie

于 2013-09-04T12:50:11.420 回答