环境变量.JAVA_HOME

1、资料:(我的文章)

  http://www.cnblogs.com/cppskill/p/8341263.html

2、

  2.1、界面

  2.2、代码

 

unit formMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Registry, IniFiles;

type
  TfrmMain = class(TForm)
    btnRegeditWrite: TButton;
    Memo1: TMemo;
    btnRegeditRead: TButton;
    edtJavaHome: TEdit;
    procedure btnRegeditWriteClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnRegeditReadClick(Sender: TObject);
  public
    function ReadString( _dwRootKey :DWORD; const _strPath, _strKey :string; var _strOut : string):Integer;
    function WriteString(_dwRootKey :DWORD; const _strPath, _strKey, _strValue :string):Integer;
  public
    function ReadIni():integer;
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

function TfrmMain.ReadString(_dwRootKey :DWORD; const _strPath, _strKey: string; var _strOut: string): Integer;
var reg :TRegistry;
begin
  Result := 0;
  reg := TRegistry.Create;
  try
    reg.RootKey := _dwRootKey;//HKEY_LOCAL_MACHINE; //'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
    if not reg.OpenKey( _strPath, false ) then
    begin
      Result := -1;
      Exit;
    end;

    _strOut := reg.ReadString(_strKey);
  finally
    reg.CloseKey;
    reg.Destroy;
  end;
end;

function TfrmMain.WriteString(_dwRootKey: DWORD; const _strPath, _strKey, _strValue: string): Integer;
var reg :TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := _dwRootKey;//HKEY_LOCAL_MACHINE; //'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
    if not reg.OpenKey( _strPath, false ) then
    begin
      Result := -1;
      Exit;
    end;

    reg.WriteString( _strKey, _strValue );
  finally
    reg.CloseKey;
    reg.Destroy;
  end;
end;


procedure TfrmMain.FormCreate(Sender: TObject);
begin
  ReadIni;
  btnRegeditReadClick(nil);
end;

procedure TfrmMain.btnRegeditWriteClick(Sender: TObject);
var strValue :string;
begin
  Memo1.Lines.Add('');
// HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Session Manager\Environment\path
// HKEY_CURRENT_USER \Environment
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment (官方)

  //strValue := 'C:\Program Files\Java\jdk1.8.0_152;AAA';
  //strValue := 'C:\Program Files\Java\jdk1.8.0_152';
  strValue := trim(edtJavaHome.Text);
  if (strValue = '') then
  begin
    Memo1.Lines.Add('新的 JAVA_HOME 的值为空 .');
    Exit;
  end;
  
  if 0>WriteString(HKEY_LOCAL_MACHINE,
    'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'JAVA_HOME', strValue) then
  begin
    Memo1.Lines.Add('注册表写入失败 .');
    Exit;
  end;
  Memo1.Lines.Add('注册表写入成功 .');
end;

procedure TfrmMain.btnRegeditReadClick(Sender: TObject);
var strValue :string;
begin
  Memo1.Lines.Add('');

  if (0 > ReadString(HKEY_LOCAL_MACHINE,
    'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'JAVA_HOME', strValue)) then
  begin
    Memo1.Lines.Add('读取 注册表 出错');
    Exit;
  end;
  Memo1.Lines.Add('HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\JAVA_HOME');
  Memo1.Lines.Add('   '+strValue);
end;

function TfrmMain.ReadIni(): integer;
var ini :Tinifile;
    strX86, strX64 :string;
    strFileName :string;
begin
  strFileName := ExtractFilePath(Paramstr(0)) + '_EnvironmentVariable.ini';
  ini := Tinifile.Create(strFileName);
  try
    strX86 := ini.ReadString('JAVA_HOME', 'Win7x86', '');
    strX64 := ini.ReadString('JAVA_HOME', 'Win7x64', '');

    Memo1.Lines.Add('JAVA_HOME(Win7x86) : '+strX86);
    Memo1.Lines.Add('JAVA_HOME(Win7x64) : '+strX64);
  finally
    ini.Free;
  end;
end;

end.

 

  2.3、ini文件(_EnvironmentVariable.ini)

 

 

3、

4、

5、

 

posted @ 2018-01-24 14:26  CodeSkill  阅读(418)  评论(0编辑  收藏  举报