加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Win CE 下 VB NET 注册表 修改

发布时间:2020-12-16 23:20:31 所属栏目:大数据 来源:网络整理
导读:Maarten Struys PTS Software July 2003 Introduction With the availability of the OpenNETCF.Registry library it is very easy to access the Windows CE registry. The functionality provided in the Registry library allows you to access the syste

Maarten Struys
PTS Software
July 2003

Introduction

With the availability of the OpenNETCF.Registry library it is very easy to access the Windows CE registry. The functionality provided in the Registry library allows you to access the system registry similar to the way this is done in the full .NET Framework using the RegistryKey class of the Microsoft.Win32 namespace. The names of the classes and methods of the OpenNETCF Registry library are identical to those of the full .NET Framework and the functionality also closely resembles that of the .NET Framework. Using a very simple application we demonstrate how to create or open a registry key and how to store and retrieve values of the specified registry key.

OpenNETCF Registry Library functionality

To classes of the OpenNETCF.Registry Library are defined inside the OpenNETCF.Win32 namespace. They match the Microsoft.Win32 namespace on the full .NET Framework. The classes inside the OpenNETCF.Win32 namespace manipulate the system registry. Two different classes are available: Registry and RegistryKey . The Registry class contains the set of standard root keys that can be found in the Windows CE registry. This class exposes the following root registry keys:

  • CurrentUser - Used to store user preferences
  • LocalMachine - Used to store configuration information for the local machine
  • ClassesRoot - Used to store information about types (classes) and their properties
  • Users - Used to store information about the default user configuration

    The Registry class contains a number of static methods,each returning a RegistryKey instance to one of the four root keys.

    The RegistryKey class represents a key node in the Windows CE registry. To open or create a particular key node you have to call a member function of a valid instance of the RegistryKey class. That is where the static member functions of the Registry come in. Let's suppose we want to create a new registry key under the LocalMachine root key. The following code snippet will do so if the registry key does not yet exist,otherwise it simply opens the existing registry key: // Create / Open a registry key to store / retrieve values or to open / create other keys.
    const string keyName = "Software//OpenNETCFDemo//LastInvoked";
    RegistryKey registryKey = Win32.Registry.LocalMachine.CreateSubKey(keyName);

    Once we have a valid instance of a RegistryKey class we can use it to create or open new keys under the currently opened registry key. You can compare this to creating or accessing nodes in a tree under a particular root. We can also set values or retrieve values that are stored under the specified registry key. It is possible to delete keys and values. Finally it is also possible to get a list of all keys or values that are stored under the currently opened registry key.

    A simple demo application

    Suppose we want to show the date and time an application was last started when starting an application. To store and retrieve this kind of information,the registry is an ideal placeholder. For demonstration purposes we build a small SDA Forms application that shows the previous date and time the application was started in a static control. Using such a simple scenario we have a little code overhead as possible and we can just concentrate on accessing the registry database. The downloadable source code is targeted for a PocketPC 2003 emulator,but the same code can easily be executed on any Windows CE device that has the .NET Compact Framework installed.

    First we create a new smart device application. In the form designer,add an empty label control and rename this to lblInvocationTime . This will be the placeholder to display the last invocation time that we are going to read from the system registry.

    Note that we modified the title of the form,using the Form properties and that we allow the application to close instead of to minimize it. This can be achieved by setting the Form's MinimizeBox property to False.

    To obtain the date and time the application was started previously we maintain a system registry value under the following key:

    /LocalMachine/Software/OpenNETCFSample/LastInvoked

    The value that we store under this key is called TimeStamp . It present it will hold a string containing the last date and time the application was started in the following format: dd-m-yyyy - hh:mm . We use exactly the value read from the registry database to inform the user about the last invocation time of the application.

    Before you can make use of the OpenNETCF Registry Library it is necessary to add a reference to the component assembly containing the library. To do so right click References in the solution explorer view of Visual Studio and add the Registry Library DLL. Registry and RegistryKey can be found in the OpenNETCF.Win32 namespace.

    To be able to access the system registry we define a private member variable of type RegistryKey in the Form class. Because we want to show the last invocation time immediately when the form is displayed,we add the functionality to access the system registry in the Form1_Load event. This event is fired when the form is loaded. The following code snippet shows the Form1_Load event handler:

    private void Form1_Load(object sender,System.EventArgs e)
    {
    const string keyName = "Software//OpenNETCFSample//LastInvoked";
    const string valueName = "TimeStamp";
    string accessTime;

    // Create / Open a registry key to store / retrieve last application start time.
    registryKey = Win32.Registry.LocalMachine.CreateSubKey(keyName);

    if (registryKey.ValueCount == 0)
    {
    // No value available in the created / opened subkey
    lblInvocationTime.Text = "Unknown";
    }
    else
    {
    // Since we use this key exclusively for this application,we can safely
    // assume that the value we are interested in is available in the registry.
    // GetValue reads the registry value and returns it to the caller.
    accessTime = (string)registryKey.GetValue(valueName);
    lblInvocationTime.Text = accessTime;
    }

    // Since we now know for sure we have a open registry key,let's store the current
    // time in it,to be able to retrieve it the next time we start the application.
    accessTime = DateTime.Now.ToShortDateString() + " - " + DateTime.Now.ToShortTimeString();
    registryKey.SetValue(valueName,accessTime);

    // Make sure to close the open registryKey. Since the RegistryKey class P/Invokes
    // to Win32 API's,a handle to a registry key remains open if we not explicitely
    // close it,even if our RegistryKey instance gets out of scope and gets GC'd.
    registryKey.Close();
    }

    To access the registry we use a constant string containing keyName and a constant string containing the valueName . When the specified key does not exist,CreateSubKey creates the key in the system registry under the specified root key,LocalMachine in this case. When the specified key already exists,CreateSubKey simply opens that key to be able to access sub keys or values.

    The RegistryKey class contains a ValueCount property that returns the number of values that exist under the open sub key. When ValueCount equals 0,no values are found under the sub key. In our sample this means that the application is activated for the very first time.

    If ValueCount unequals 0,a value is available. Since we maintain the registry key exclusively we assume that the value we are looking for is available in this case.

    Using GetValue we simply retrieve the registry value. Since GetValue returns an Object we have to cast it to a string,because our stored value is of type string .

    We now can simply assign the retrieved value to the Text property of lblInvocationTime . After retrieving the registry value we store the current date and time back into the system registry for the next time the application is invoked.

    SetValue uses the valueName to create a new value or to overwrite an existing value. This is all there is to store data in the system registry and to retrieve data from the system registry.

    To keep the sample code simple we have ignored the fact that RegistryKey members can throw exceptions. A "real" application might want to catch those exceptions to gracefully exit or to recover from failures. Special attention is needed to properly close open registry keys.

    Every time an instance of RegistryKey opens or creates a registry key,an underlying Win32 API is called that returns a handle to the specified registry key. Even though we don't have to worry about cleaning up resources in managed code,we have to take care of cleaning up resources in unmanaged code. That is the reason why we explicitly need to call the Close member function of the RegistryKey class.

    There is one exception to this. Even though it is possible in code,you should not close the four available root keys that are defined as static members in the Registry class,inside an application. After closing them it is not possible to re-open the root keys again without restarting the application.

    Conclusion

    Using the OpenNETCF Registry Library you get full access to the Windows CE system registry without the need to P/Invoke the Win32 registry API's. The functionality is similar to the functionality of the Registry classes of the full .NET Framework. As the sample code showed,accessing the system registry is fairly easy and straight forward.

    Sample code

    Sample code for this article can be downloaded here .

  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读