<html><head><title> IniConfig</title>
<script language=javascript>
String.prototype.trim = function(){ return this.replace(/(^/s+)|(/s+$)/g,'');};
IniConfig=function(iniFileName){ this.iniFileName = iniFileName; this._iniSecDictionary = new Array(); this.fso = new ActiveXObject("Scripting.FileSystemObject");};
IniConfig.prototype._checkFile = function(){ if(!this.fso.FileExists(this.iniFileName)) { this.fso.CreateTextFile(this.iniFileName,true,true); }};
IniConfig.prototype.load = function(){ this._checkFile(); var currSecName = null; var fs = this.fso.OpenTextFile(this.iniFileName,1,false,-1); while(!fs.AtEndOfStream) { var strLine = fs.ReadLine().trim(); if(strLine.length > 0) { var firchCh = strLine.substr(0,1); if(firchCh != ';') { if(firchCh == '[') { var secName = strLine.substr(1,strLine.length - 2); currSecName = secName; this._iniSecDictionary[secName] = new Array(); } else { var idx = strLine.indexOf('+'); var strKey = strLine.substring(0,idx); var strVal = strLine.substr(idx + 1);
if(currSecName == null) { throw("Ini文件格式不正确!"); } this._iniSecDictionary[currSecName][strKey] = strVal; } } } }
fs.Close(); fs = null;};
IniConfig.prototype.save = function(){ this._checkFile(); var dic = this._iniSecDictionary; var currSecName = null; var fs = this.fso.OpenTextFile(this.iniFileName,2,true,-1); for(var sec in dic) { fs.WriteLine('[' + sec + ']'); alert('[' + sec + ']'); for(var key in dic[sec]) { fs.WriteLine(key + '+' + dic[sec][key]); alert(key + '+' + dic[sec][key]); } }
fs.Close(); fs = null;};
IniConfig.prototype.get = function(secName,keyName){ var dic = this._iniSecDictionary; try { return dic[secName][keyName]; } catch(e) { return ''; }};
IniConfig.prototype.set = function(secName,keyName,val){
var dic = this._iniSecDictionary; try { if(dic[secName] == null) { dic[secName] = new Array(); } dic[secName][keyName] = val; } catch(e) { alert(e.message); }};
function $(objID){ return document.getElementById(objID);}
</script>
<script language=javascript> //初始化对象 var iniFileObj = null; var iniFilePath = ""; iniFilePath = "C://Ini1.ini"; //加载文件,如果失败则创建文件 try { iniFileObj = new IniConfig(iniFilePath); iniFileObj.load(); } catch(e) { alert(e.message); $("idBtnGetValues").enable = false; $("idBtnSetValues").enable = false; } //获取值 function getValues(){ var sSeg = null; var sKey = null; var sValue = null; sSeg = $("idSeg").value; sKey = $("idKey").value; sValue = iniFileObj.get(sSeg,sKey); $("idValue").value = sValue; alert("获取值:" + sValue); } //设置值 function setValues(){ var sSeg = null; var sKey = null; var sValue = null; sSeg = $("idSeg").value; sKey = $("idKey").value; sValue = $("idValue").value; iniFileObj.set(sSeg,sKey,sValue); iniFileObj.save(); alert("设置值成功。"); } </script>
</head><body> Segment:<input id="idSeg" value=SEG1 /><br> Keys:<input id="idKey" value=KEY1 /><br> Values:<input id="idValue" value=Value1 /><br> <input type=button id="idBtnGetValues" Value="getValues()" onClick="getValues();" /> <input type=button id="idBtnSetValues" Value="setValues()" onClick="setValues();" /></body></html>