码迷,mamicode.com
首页 > 编程语言 > 详细

环境变量.JAVA_HOME

时间:2018-01-24 15:33:44      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:skill   ini文件   src   manage   session   registry   dct   free   htm   

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、

 

环境变量.JAVA_HOME

标签:skill   ini文件   src   manage   session   registry   dct   free   htm   

原文地址:https://www.cnblogs.com/CodeSkill/p/8341464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!