001/*
002 * Copyright 2005,2009 Ivan SZKIBA
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.ini4j.tutorial;
017
018import org.ini4j.Reg;
019
020import static org.junit.Assert.*;
021
022import java.io.File;
023import java.io.IOException;
024
025//<editor-fold defaultstate="collapsed" desc="apt documentation">
026//|
027//|                -------------------------
028//|                Windows Registry Tutorial
029//|
030//|Windows Registry Tutorial - Read/Write windows registry
031//|
032//| Yes, it is possible now to read/write registry from java programs
033//| without native (JNI) code !
034//|
035//</editor-fold>
036public class WindowsRegistryTutorial extends AbstractTutorial
037{
038    public static final String FILENAME = "../sample/dwarfs.reg";
039
040    public static void main(String[] args) throws Exception
041    {
042        if (Reg.isWindows())
043        {
044            new WindowsRegistryTutorial().run(filearg(args));
045        }
046    }
047
048    @Override protected void run(File arg) throws Exception
049    {
050        sample01();
051        sample02();
052        sample03();
053    }
054
055//|
056//|* Write
057//|
058//| Lets write something to registry
059//{
060    void sample01() throws IOException
061    {
062        Reg reg = new Reg();
063        Reg.Key key = reg.add("HKEY_CURRENT_USER\\hello");
064
065        key.put("world", "Hello World !");
066        reg.write();
067//}
068//| This code will create a "hello" key in HKEY_CURRENT_USER hive, and
069//| put "Hello World !" with name "world".
070    }
071
072//|
073//|* Read
074//|
075//| Lets read something from Control Panel settings...
076//{
077    void sample02() throws IOException
078    {
079        Reg reg = new Reg("HKEY_CURRENT_USER\\Control Panel");
080        Reg.Key cp = reg.get("HKEY_CURRENT_USER\\Control Panel");
081        Reg.Key sound = cp.getChild("Sound");
082        String beep = sound.get("Beep");
083
084//}
085//|
086    }
087
088//|
089//|* Create environment variable
090//|
091//| Lets create a new environment variable under current users environment....
092//{
093    void sample03() throws IOException
094    {
095        Reg reg = new Reg();
096        Reg.Key env = reg.add("HKEY_CURRENT_USER\\Environment");
097
098        env.put("SAMPLE_HOME", "c:\\sample");
099        reg.write();
100//}
101//| Thats it ! Now your environment contains variable SAMPLE_HOME ! Unfortunetly
102//| you have to restart Windows to see this variable.... but hey, we crated new
103//| environment variable from java without any native code !
104    }
105}