CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2008
    Posts
    18

    checking for a value in the registry

    I have my code it works i get back the registry value but what i want to do is

    check to see if its empty if its empty ok to install program

    if not would you like to remove the registry value?

    here is my code it dosnt work if i put somthing in the reg value and run my program it dosnt check
    if i remove the data from that value same results.

    how can i set this up below is my code

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Win32;
    
    
    namespace regMod
    {
        class Program
        {
            static void Main(string[] args)
            {
                 
    
                    RegistryKey MyReg = Registry.LocalMachine.OpenSubKey
                        ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\setup.exe", true);
    
                    try
                    {
    
                        string IsVal = (string)MyReg.GetValue("Default", "");
    
                        Console.WriteLine(MyReg.GetValue(IsVal).ToString());
    
                        if (IsVal == null)
                        {
    
                            Console.WriteLine("ok to install");
                        }
                        else
                        {
                            Console.WriteLine("Remove key value data?");
    
                        }
                    }



    thank you for your time
    -summey
    Last edited by summey; April 26th, 2009 at 08:49 PM.

  2. #2
    Join Date
    Dec 2002
    Location
    at home and at office :D
    Posts
    126

    Re: checking for a value in the registry

    Quote Originally Posted by summey View Post
    I have my code it works i get back the registry value but what i want to do is

    check to see if its empty if its empty ok to install program

    if not would you like to remove the registry value?

    here is my code it dosnt work if i put somthing in the reg value and run my program it dosnt check
    if i remove the data from that value same results.

    how can i set this up below is my code
    sorry, I don't get it, what do you mean by ' it dosnt check' if you put smth in the reg value? Which part of the if-clause is run through?
    Better ask and make a rhyme
    than search dead threads for a long time.

  3. #3
    Join Date
    Feb 2009
    Posts
    56

    Re: checking for a value in the registry

    You open without close, becareful of bacteria entry

  4. #4
    Join Date
    Dec 2008
    Posts
    18

    Re: checking for a value in the registry

    please check this image out www.summeylabs.com/images/reg.jpg

    to show you what im talking about visually.

    i just want it to check to see if the the value has data if the data is empty then print ok if the data isnt emptoy print not empty

    my program dosnt work if i have data in the there or not still prints the same answer.

    Thank you for your time.

    -Summey

  5. #5
    Join Date
    Dec 2002
    Location
    at home and at office :D
    Posts
    126

    Re: checking for a value in the registry

    http://msdn.microsoft.com/en-us/library/xzxd58z1.aspx
    A registry key can have one value that is not associated with any name. When this unnamed value is displayed in the registry editor, the string "(Default)" appears instead of a name. To retrieve this unnamed value, specify either null/Nothing/nullptra null reference (Nothing in Visual Basic) or the empty string ("") for name.
    Better ask and make a rhyme
    than search dead threads for a long time.

  6. #6
    Join Date
    Dec 2008
    Posts
    18

    Re: checking for a value in the registry

    Thank you for all the advice. I got it working and how i did it was.

    its not null when you set it to "" because it puts "" in binary more zeros left be hind thus leaving invisible data.

    so if i right click on (default) in the reg and hit delete it sets the value field to (value not set) thus making it null and my program works.

    my questions is how do i properly delete it to so its back to value not set ?

    everything works



    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Win32;
    
    
    namespace regMod
    {
        class Program
        {
            static void Main(string[] args)
            {
    
          
    
                    RegistryKey MyReg = Registry.LocalMachine.OpenSubKey
                        ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\setup.exe", true);
    
                    string IsVal = (string)MyReg.GetValue("Default");
                
                    object ValData = MyReg.GetValue(IsVal);
    
    
                    if (ValData != null)
                    {
    
                        string stringData = ValData.ToString();
                        Console.WriteLine(MyReg.GetValue(IsVal).ToString());
    
                        Console.WriteLine("removing value now");
                        
                        MyReg.SetValue("","");
                        MyReg.Close();
    
    
                    }
                    else
                    {
    
                        Console.WriteLine("Empty");
                        MyReg.Close();
    
                    }      
            }
        }
    }
    thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured