如何将实现这样的事情在C#?

object Registry;
Registry = MyProj.Registry.Instance;

int Value;
Value = 15;
Registry.Value = Value; /* Sets it to 15 */
Value = 25;
Value = Registry.Value; /* Returns the 15 */

到目前为止,我有此对象:

namespace MyProj
{
    internal sealed class Registry
    {
        static readonly Registry instance = new Registry();

        static Registry()
        {
        }

        Registry()
        {
        }

        public static Registry Instance
        {
            get
            {
                return instance;
            }
        }
    }
}
有帮助吗?

解决方案

只是一个属性添加到您的注册类:

internal sealed class Registry
{
    public int Value { get; set; }
    ...
}

然后,使用这样的:

Registry theRegistry = MyProj.Registry.Instance;
//note: do not use object as in your question

int value = 15;
theRegistry.Value = value; /* Sets it to 15 */
value = 25;
value = theRegistry.Value; /* Returns the 15 */

其他提示

您应该使用 Microsoft.Win32.Registry 类。它和相关的类,都需要使用Windows注册表来工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top