访问注册表--通过VJ6来处理Windows注册表2


测试程序清单:

import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;

/*
*This class can take a variable number of parameters on the command line. Program
*execution begins with the main() method. The class constructor is not invoked unless
*an object of type "Form1" created in the main() method
*/
public class frmRegistryTest extends Form
{
        //Root registry key the user chooses.
        RegistryKey objRootKey;
        //Name of root key, for error messages.

        Public frmRegistryTest()
        {
            //Required for Visual J++ Forms Designer support.
            initForm();

            //Initialize form values to use the HKEY_CURRENT_USER
            //registry key and the Software subkey.
            RbRoot2.setChecked(true);
            txtSubKey.setText("Software");
            txtApplication.setText("RegistryTest");
            txtSection.setText("Configuration");
            txtName.setText("TestValue");
            txtValue.setText("");
        }
       
        //The main entry point for the application
        //
        //@param args Array of parameters passed to the application via the command line.
        public static void main(String args[])
        {
            Application.run(new frmRegistryTest());
        }

        private void btnGetSetting_click(Object sender, Event e)
        {
            txtValue.setText(getSetting());
        }
       
        private void btnSaveSetting_click(Object sender, Event e)
        {
            saveSetting();
        }

        private void btnDeleteSetting_click(Object sender, Event e)
        {
            deleteSetting();
        }

        private void alertNoSubKey(String strSubKey)
        {
            int intAction = MessageBox.show("Registry SubKey: '" +
                strSubKey + "' Not Found.", "No Registry Entry.");
            return;
        }

        private String composeSubKey()
        {

            //Takes text from edit boxes on the form.
            //Creates a registry SubKey string.
            String strSubKey;
            String strTemp;
   
            StrSubKey = txtSubKey.getText();
   
            //If the application name is not an empty string,
            //add it to the subkey.
            StrTemp = txtApplication.getText();
            If (strTemp.length() != 0)
                strSubKey = strSubKey + "\\" + strTemp;

            //If the section name is not an empty string,
            //add it to the subkey.
            StrTemp = txtSetion.getText();
            If (strTemp.length() != 0)
                strSubKey = strSubKey + "\\" + strTemp;
           
            return strSubKey;
        }

        private String getSetting()
        {
            String strSetting;

            //Set the registry to the appropriate SubKey.
            //Opens the SubKey in read-only mode for safety.
            RegistryKey objRegistryKey =   
                ObjRootKey.getSubKey(composeSubKey(), true);
            if(objRegistryKey = = null)
                alertNoSubKey(strRootKey + composeSubKey());
            else
            {
                strSetting = objRegistryKey.getValue(
                    txtName.getText(), "").toString();
                objRegistryKey.close();
                return strSetting;
            }
       
            return "";
        }

        private void saveSetting()
        {
            //Set the registry to the appropriate SubKey.
            //Creates the SubKey,if it doesn't already exist.
            RegistryKey objRegistryKey =
                ObjRootKey.createSubKey(composeSubKey());
            objRegistryKey.setValue(txtName.getText(),
                                txtValue.getText());
            objRegistryKey.close();
        }

        private void deleteSetting()
        {
            //Set the registry to the appropriate SubKey.
            //Delete the SubKey value.
            RegistryKey objRegistryKey =
                ObjRootKey.getSubKey(composeSubKey(), false);
            If (objRegistryKey = = null)
                AlertNoSubKey(strRootKey + composeSubKey());
            else
            {
                objRegistryKey.deleteValue(txtName.getText());
                objRegistryKey.close();
                txtName.setText("");
                txtValue.setText("");
            }
        }

        private void rbRoot1_checkedChanged(Object source, Event e)
        {
            if (rbRoot1.getChecked())
            {
                objRootKey = Registry.CLASSES_ROOT;
                strRootKey = "HKEY_CLASSES_ROOT\\";
            }
        }

    private void rbRoot2_checkedChanged(Object source, Event e)
        {
            if (rbRoot2.getChecked())
            {
                objRootKey = Registry.CURRENT_USER;
                strRootKey = "HKEY_CURRENT_USER\\";
            }
        }

    private void rbRoot3_checkedChanged(Object source, Event e)
        {
            if (rbRoot3.getChecked())
            {
                objRootKey = Registry.LOCAL_MACHINE;
                strRootKey = "HKEY_LOCAL_MACHINE\\";
            }
        }

    private void rbRoot4_checkedChanged(Object source, Event e)
        {
            if (rbRoot4.getChecked())
            {
                objRootKey = Registry.USER;
                strRootKey = "HKEY_USER\\";
            }
        }
}

/**
* NOTE: The folowing code is required by the Visual J++
    * Forms Designer. It can be modified using the form editor. Do not modify it using the code
* editor.
Container components = new Container();
GroupBox grpRoot = new GroupBox();
protected RadioButton rbRoot1 = new RadioButton();
protected RadioButton rbRoot2 = new RadioButton();
protected RadioButton rbRoot3 = new RadioButton();
protected RadioButton rbRoot4 = new RadioButton();
protected Button btnGetSetting = new Button();
protected Button btnSavesetting = new Button();
protected Button btnDeletesetting = new Button();
Label lblSubKey = new Label();
protected Edit txtSubKey = new Edit();
Label lblApplication = new Label();
Label lblSection = new Label();
Label lblName = new Label();
Label lblValue = new Label();
protected Edit txtApplication = new Edit();
protected Edit txtSection = new Edit();
protected Edit txtName = new Edit();
protected Edit txtValue = new Edit();

private void initForm()
{
        this.setText("Registry Test");
        this.setAutoScaleBaseSize(13);
        this.setClientSize(new Point(500, 190));
       
        grpRoot.setLocation(new Point(5, 1));
        grpRoot.setSize(new Point(160, 102));
        grpRoot.setTabIndex(0);
        grpRoot.setTabStop(false);
        grpRoot.setText("Root Key");

        rbRoot1.setLocation(new Point(10, 20));
        rbRoot1.setSize(new Point 148, 12));
        rbRoot1.setTabIndex(0);
        rbRoot1.setText("HKEY_CLASSES_ROOT");
        rbRoot1.addOnCheckedChanged(new EventHandler(this.rbRoot1_checkedChanged());

        rbRoot2.setLocation(new Point(10, 40));
        rbRoot2.setSize(new Point 148, 12));
        rbRoot2.setTabIndex(1);
        rbRoot2.setText("HKEY_CURRENT_USER");
        rbRoot2.addOnCheckedChanged(new EventHandler(this.rbRoot2_checkedChanged());

        rbRoot3.setLocation(new Point(10, 60));
        rbRoot3.setSize(new Point 148, 12));
        rbRoot3.setTabIndex(2);
        rbRoot3.setText("HKEY_LOCAL_MACHINE");
        rbRoot3.addOnCheckedChanged(new EventHandler(this.rbRoot3_checkedChanged());

        rbRoot4.setLocation(new Point(10, 80));
        rbRoot4.setSize(new Point 148, 12));
        rbRoot4.setTabIndex(3);
        rbRoot4.setText("HKEY_USERS");
        rbRoot4.addOnCheckedChanged(new EventHandler(this.rbRoot4_checkedChanged());

        btnGetSetting.setLocation(new Point(176, 8));
        btnGetSetting.setSize(new Point(74, 25));
        btnGetSetting.setTabIndex(10);
        btnGetSetting.setText("Get Setting");
        btnGetSetting.addOnClick(new EventHandler(this.btnGetSetting_click));

        btnGetSetting.setLocation(new Point(176, 40));
        btnGetSetting.setSize(new Point(74, 25));
        btnGetSetting.setTabIndex(11);
        btnGetSetting.setText("Save Setting");
        btnGetSetting.addOnClick(new EventHandler(this.btnGetSetting_click));

        btnGetSetting.setLocation(new Point(176, 72));
        btnGetSetting.setSize(new Point(74, 25));
        btnGetSetting.setTabIndex(12);
        btnGetSetting.setText("Delete Setting");
        btnGetSetting.addOnClick(new EventHandler(this.btnGetSetting_click));

        lblSubKey.setLocation(new Point(7, 108));
        lblSubKey.setSize(new Point(80, 12));
        lblSubKey.setTabIndex(1);
        lblSubKey.setTabStop(false);
        lblSubKey.setText("");

        txtSubKey.setLocation(new Point(7, 124));
        txtSubKey.setSize(new Point(490, 20);
        txtSubKey.setTabIndex(4);
        txtSubKey.setText("");

        lblApplication.setLocation(new Point(7, 144));
        lblApplication.setSize(new Point(80, 12));
        lblApplication.setTabIndex(2);
        lblApplication.setTabStop(false);
        lblApplication.setText("Application:");

        lblSection.setLocation(new Point(114, 144));
        lblSection.setSize(new Point(80, 12));
        lblSection.setTabIndex(3);
        lblSection.setTabStop(false);
        lblSection.setText("Section");

        lblName.setLocation(new Point(222, 144));
        lblName.setSize(new Point(80, 12));   
        lblName.setTabIndex(9);
        lblName.setTabStop(false);
        lblName.setText("Name:");

        lblValue.setLocation(new Point(329, 144));
        lblValue.setSize(new Point(80, 12));
        lblValue.setTabIndex(13);
        lblValue.setTabStop(false);
        lblValue.setText("Value:");

        txtApplication.setLocation(new Point(7, 157));
        txtApplication.setSize(new Point(80, 12));
        txtApplication.setTabIndex(5);
        txtApplication.setText("");

        txtSection.setLocation(new Point(114, 157));
        txtSection.setSize(new Point(100, 20));
        txtSection.setTabIndex(6);
        txtSection.setText("");

        txtName.setLocation(new Point(222, 157));
        txtName.setSize(new Point(100, 20));
        txtName.setTabIndex(7);
        txtName.setText("");
       
txtValue.setLocation(new Point(329, 157));
txtValue.setSize(new Point(100, 20));
txtValue.setTabIndex(8);
txtValue.setText("");

this.setNewControls(new Control[]{
        txtSubKey;
        lblSubKey;
        txtValue;
        txtName;
        txtSection;
        txtApplication;
        lblValue;
        lblName;
        lblSection;
        lblApplication;
        btnDeleteSetting;
        btnSaveSetting;
        btnGetSetting;
        grpRoot});
grpRoot.setNewControls(new Control[]{
        rbRoot4,
        rbRoot3,
        rbRoot2,
        rbRoot1});
}
//NOTE: End of Forms Designer support code.

Public static class ClassInfo extends Form.ClassInfo
{
}
}
最后编辑2005-09-11 21:07:57