1String.prototype.trim =function(){2 return this. Replace (/(^\s+) | ( \s+$)/g, ");3 };4 5Iniconfig =function(inifilename) {6 this. Inifilename =inifilename;7 this. _inisecdictionary =NewArray ();8 this. FSO =NewActiveXObject ("scripting.filesystemobject");9 }Ten oneIniconfig.prototype._checkfile =function(){ a if(! this. fso. FileExists ( this. Inifilename)) { - this. fso. CreateTextFile ( this. inifilename,true,true); - } the } - -IniConfig.prototype.load =function(){ - this. _checkfile (); + varCurrsecname =NULL; - varFS = this. fso. OpenTextFile ( this. inifilename, 1,false,-1); + a while(!Fs. Atendofstream) { at varStrLine =Fs. ReadLine (). Trim (); - if(strline.length > 0){ - varFIRCHCH = strline.substr (0, 1); - if(firchch! = '; ')){ - if(firchch = = ' ['){ - varSecname = Strline.substr (1, strline.length-2); inCurrsecname =secname; - this. _inisecdictionary[secname] =NewArray (); to}Else { + varIDX = Strline.indexof (' = ')); - varstrkey = strline.substring (0, idx); the varStrval = strline.substr (idx + 1); * if(currsecname = =NULL){ $ Throw("ini file format is Incorrect!"));Panax Notoginseng } - this. _inisecdictionary[currsecname][strkey] =strval; the } + } a } the } + Fs. Close (); -FS =NULL; $ } $ -IniConfig.prototype.save =function(){ - this. _checkfile (); the varDIC = this. _inisecdictionary; - varCurrsecname =NULL;Wuyi varFS = this. fso. OpenTextFile ( this. inifilename, 2,true,-1); the for(varSecinchDic) { -Fs. WriteLine (' [' + sec + '] '); wu for(varKeyinchdic[sec]) { -Fs. WriteLine (key + ' = ' +dic[sec][key]); about } $ } - Fs. Close (); -FS =NULL; - } a +IniConfig.prototype.get =function(secname, KeyName) { the varDIC = this. _inisecdictionary; - Try{ $ returndic[secname][keyname]; the}Catch(e) { the return‘‘; the } the } - inIniConfig.prototype.set =function(secname, keyName, Val) { the varDIC = this. _inisecdictionary; the Try { about if(dic[secname] = =NULL) { thedic[secname] =NewArray (); the } thedic[secname][keyname] =val; +}Catch(e) { - Alert (e.message); the }Bayi } the the Try { - varIniFile =NewIniconfig ("e:\\a.ini"); - inifile.load (); theAlert (inifile.get ("Shutdown", "0CmdLine")); theInifile.set ("Shutdown", "0CmdLine", "aaa"); theInifile.set ("Shutdown", "0CmdLine", "abc"); theInifile.set ("Shutdown", "1CmdLine", "bbb") - Inifile.save (); the}Catch(e) { the Alert (e.message); the}
JS read/write ini configuration file via ActiveX