I'm trying to work with STATIC properties in C#. I have some properties I
get from the registry, so I don't want to instance the class.
I have something like this, but I cannot get it to work. Whenever I try to
access these properties from other classes or project in the same solution
(referenced, of course), I cannot find them...

Would anybody be so kind to give me a hand?

Thanks a lot!!!
----

namespace MyLibrary
{
public static class Config
{
private RegistryKey regMySoft =
Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft");

public static String MailServer
{
get
{
return regSoft.GetValue("mailServer").ToString();
}
set
{
regSoft.SetValue("mailServer", value);
}
}
}
}

Re: Static Proerties in C# by Morten

Morten
Sat Jun 09 05:28:32 CDT 2007

On Fri, 08 Jun 2007 23:59:37 +0200, Carlos Sosa Albert <betun <"hotmail>=
"> wrote:

> I'm trying to work with STATIC properties in C#. I have some propertie=
s I
> get from the registry, so I don't want to instance the class.
> I have something like this, but I cannot get it to work. Whenever I tr=
y to
> access these properties from other classes or project in the same solu=
tion
> (referenced, of course), I cannot find them...
>
> Would anybody be so kind to give me a hand?
>
> Thanks a lot!!!
> ----
>
> namespace MyLibrary
> {
> public static class Config
> {
> private RegistryKey regMySoft =3D
> Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft");
>
> public static String MailServer
> {
> get
> {
> return regSoft.GetValue("mailServer").ToString();
> }
> set
> {
> regSoft.SetValue("mailServer", value);
> }
> }
> }
> }
>
>

Nothing really wrong about your code (regMySoft needs to be static thoug=
h), but you may want to check for the existance of the key, and if you n=
eed to create it, the registry key needs to have write permissions.

public static class Config
{
private static RegistryKey key =3D Registry.LocalMachine.OpenSu=
bKey("SOFTWARE\\MySoft", true);
private static RegistryKey softwareKey =3D Registry.LocalMachin=
e.OpenSubKey("SOFTWARE", true);

public static string MailServer
{
get
{
if (key =3D=3D null)
{
softwareKey.CreateSubKey("MySoft");
key =3D softwareKey.OpenSubKey("MySoft", true);
key.SetValue("mailServer", "Default value");
}
return key.GetValue("mailServer").ToString();
}
set { key.SetValue("mailServer", value); }
}
}


-- =

Happy coding!
Morten Wennevik [C# MVP]

Re: Static Proerties in C# by DArnold

DArnold
Sat Jun 09 07:24:39 CDT 2007

Carlos Sosa Albert wrote:
> I'm trying to work with STATIC properties in C#. I have some properties
> I get from the registry, so I don't want to instance the class.
> I have something like this, but I cannot get it to work. Whenever I try
> to access these properties from other classes or project in the same
> solution (referenced, of course), I cannot find them...
>
> Would anybody be so kind to give me a hand?
>
> Thanks a lot!!!
> ----
>
> namespace MyLibrary
> {
> public static class Config
> {
> private RegistryKey regMySoft =
> Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft");
>
> public static String MailServer
> {
> get
> {
> return regSoft.GetValue("mailServer").ToString();
> }
> set
> {
> regSoft.SetValue("mailServer", value);
> }
> }
> }
> }


More something like this?

public class Config
{
private static RegistryKey regMySoft =
Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft");

public static String MailServer
{
get
{
return regMySoft.GetValue("mailServer").ToString();
}
set
{
regMySoft.SetValue("mailServer", value);
}
}
}
}


x = Config.MailServer;
Config.MailServer = x;

Re: Static Proerties in C# by Carlos

Carlos
Tue Jun 12 13:09:23 CDT 2007

Thanks a lot Darnold, it worked just fine!
=)

"DArnold" <DArnold@DArnold.com> wrote in message
news:OZvMBFpqHHA.4180@TK2MSFTNGP04.phx.gbl...
> Carlos Sosa Albert wrote:
>> I'm trying to work with STATIC properties in C#. I have some properties I
>> get from the registry, so I don't want to instance the class.
>> I have something like this, but I cannot get it to work. Whenever I try
>> to access these properties from other classes or project in the same
>> solution (referenced, of course), I cannot find them...
>>
>> Would anybody be so kind to give me a hand?
>>
>> Thanks a lot!!!
>> ----
>>
>> namespace MyLibrary
>> {
>> public static class Config
>> {
>> private RegistryKey regMySoft =
>> Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft");
>>
>> public static String MailServer
>> {
>> get
>> {
>> return regSoft.GetValue("mailServer").ToString();
>> }
>> set
>> {
>> regSoft.SetValue("mailServer", value);
>> }
>> }
>> }
>> }
>
>
> More something like this?
>
> public class Config
> {
> private static RegistryKey regMySoft =
> Registry.LocalMachine.OpenSubKey("SOFTWARE\\MySoft");
>
> public static String MailServer
> {
> get
> {
> return regMySoft.GetValue("mailServer").ToString();
> }
> set
> {
> regMySoft.SetValue("mailServer", value);
> }
> }
> }
> }
>
>
> x = Config.MailServer;
> Config.MailServer = x;