473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need Example; Test to see if regkey/value exists

I've been playing with the RegistryKey class in the Microsoft.Win32
namespace.
I can do various things but I can't seem to get the syntax correct to see if
a value or a key exsists.

I know I need to use the GetValue & OpenSubKey methods

I've been trying variations on these methods with no luck.
Can someone give me a working example?

This is my latest (failed) attempt

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\0000000";

using (RegistryKey Key = Registry.CurrentUser)

{
if (Key.OpenSubKey(sKey)= null)
{
}
}
Nov 16 '05 #1
5 1483
Is = supposed to be == in the if statement?
"Yogi_Bear_79" <IT*****@spamfree.com> wrote in message
news:q6********************@comcast.com...
I've been playing with the RegistryKey class in the Microsoft.Win32
namespace.
I can do various things but I can't seem to get the syntax correct to see if a value or a key exsists.

I know I need to use the GetValue & OpenSubKey methods

I've been trying variations on these methods with no luck.
Can someone give me a working example?

This is my latest (failed) attempt

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\0000000";

using (RegistryKey Key = Registry.CurrentUser)

{
if (Key.OpenSubKey(sKey)= null)
{
}
}

Nov 16 '05 #2
Well, it doesn't retunr an error, however it returns a false postive.

I'm lookin in the
HKCU\Software\Microsoft\Office\Outlook\OMI Account Manager\Accounts"
Key for subkeys that consist of 7 zeros + 01~99

In the below example it should return "Exsits" but no matter if the key is
there or not I always get Doesn't Exsist

private void button1_Click(object sender, System.EventArgs e)

{

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\00000001";

string sPort = "LDAP Port";

string sConnection = "LDAP Secure Connection";

int x;
using (RegistryKey Key = Registry.CurrentUser)

{

if (Key.OpenSubKey(sKey)==null)

{

MessageBox.Show("Doesn't Exists");

}

else

{

MessageBox.Show("Exists");

}

}


Nov 16 '05 #3
Yogi_Bear_79 <IT*****@spamfree.com> wrote:
I've been playing with the RegistryKey class in the Microsoft.Win32
namespace.
I can do various things but I can't seem to get the syntax correct to see if
a value or a key exsists.

I know I need to use the GetValue & OpenSubKey methods

I've been trying variations on these methods with no luck.
Can someone give me a working example?

This is my latest (failed) attempt

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\0000000";

using (RegistryKey Key = Registry.CurrentUser)

{
if (Key.OpenSubKey(sKey)= null)
{
}
}


For one thing, your "using" block is guarding the wrong thing there -
you don't really want to dispose of the Registry.CurrentUser key, as
that's always open. You want something like:

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(sKey))
{
if (key==null)
{
....
}
}

Note the == rather than = in the condition.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Jon,

Getting the same results.
I'm getting the Doesn't Exist message box on keys htat exist and ones
that don't exist

private void button1_Click(object sender, System.EventArgs e)

{

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\00000000";

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(sKey))

{

if (key==null)

{

MessageBox.Show("Doesn't Exist");

}

else

{

MessageBox.Show("Exists");

}

}
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Yogi_Bear_79 <IT*****@spamfree.com> wrote:
I've been playing with the RegistryKey class in the Microsoft.Win32
namespace.
I can do various things but I can't seem to get the syntax correct to see if a value or a key exsists.

I know I need to use the GetValue & OpenSubKey methods

I've been trying variations on these methods with no luck.
Can someone give me a working example?

This is my latest (failed) attempt

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\0000000";

using (RegistryKey Key = Registry.CurrentUser)

{
if (Key.OpenSubKey(sKey)= null)
{
}
}


For one thing, your "using" block is guarding the wrong thing there -
you don't really want to dispose of the Registry.CurrentUser key, as
that's always open. You want something like:

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(sKey))
{
if (key==null)
{
....
}
}

Note the == rather than = in the condition.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Yogi_Bear_79 <IT*****@spamfree.com> wrote:
Getting the same results.
I'm getting the Doesn't Exist message box on keys htat exist and ones
that don't exist

private void button1_Click(object sender, System.EventArgs e)

{

string sKey = @"\Software\Microsoft\Office\Outlook\OMI Account
Manager\Accounts\00000000";


The "\" at the start is the cause of your problems - get rid of it and
everything should be fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve | last post by:
i pulled an example off the web and modified it somewhat and whats killimg me is that sometimes it works and sometimes it doesnt. the following is the only line that allows this thing to work over...
0
by: Shapper | last post by:
Hello, I have this code in Global.asax: Sub Session_Start(Sender As Object, E As EventArgs) Dim cookie As HttpCookie = Request.Cookies("MyCookie") If Not cookie Is Nothing Then...
18
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't...
3
by: John Salerno | last post by:
Ok, I've been staring at this and figuring it out for a while. I'm close to getting it, but I'm confused by the examples: (?(id/name)yes-pattern|no-pattern) Will try to match with yes-pattern if...
1
by: sommarlov | last post by:
Hi everyone >From one of our systems an xml file is produced. I need to validate this file before we send it to an external system for a very lenghty process. I cannot change the xml file layout....
5
by: RioRanchoMan | last post by:
I have a forum table where the field Forum_ID of the first thread corresponds to itself in the field Forum_Ancestor, and 0 (zero) for the field Forum_Parent when it is the first topic in a thread:...
4
by: Gabe Moothart | last post by:
Hello, In one of my asp.net applications I test the existence of a file, like so: File.Exists(Server.MapPath("/path/file.jpg"))); This was failing, even though the path returned by...
46
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
3
by: Eric_Dexter | last post by:
I am trying to take some data in file that looks like this command colnum_1 columnum_2 and look for the command and then cange the value in the collum(word) number indicated. I am under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.