JS通過ActiveX讀寫ini設定檔

來源:互聯網
上載者:User

標籤:config   end   replace   object   cat   row   cti   message   conf   

 1   String.prototype.trim = function(){ 2     return this.replace(/(^\s+)|(\s+$)/g, ‘‘); 3   }; 4  5   IniConfig = function(iniFileName)  { 6     this.iniFileName = iniFileName; 7     this._iniSecDictionary = new Array(); 8     this.fso = new ActiveXObject("Scripting.FileSystemObject"); 9   }10 11   IniConfig.prototype._checkFile = function(){12     if (!this.fso.FileExists(this.iniFileName)){13       this.fso.CreateTextFile(this.iniFileName, true, true);14     }15   }16 17   IniConfig.prototype.load = function(){18     this._checkFile();19     var currSecName = null;20     var fs = this.fso.OpenTextFile(this.iniFileName, 1, false, -1);21 22     while (!fs.AtEndOfStream) {23       var strLine = fs.ReadLine().trim();24       if (strLine.length > 0){25         var firchCh = strLine.substr(0, 1);26         if (firchCh != ‘;‘){27           if (firchCh == ‘[‘){28             var secName = strLine.substr(1, strLine.length - 2);29             currSecName = secName;30             this._iniSecDictionary[secName] = new Array();31           } else {32             var idx = strLine.indexOf(‘=‘);33             var strKey = strLine.substring(0, idx);34             var strVal = strLine.substr(idx + 1);35             if (currSecName == null){36               throw ("Ini檔案格式不正確!");37             }38             this._iniSecDictionary[currSecName][strKey] = strVal;39           }40         }41       }42     }43     fs.Close();44     fs = null;45   }46 47   IniConfig.prototype.save = function(){48     this._checkFile();49     var dic = this._iniSecDictionary;50     var currSecName = null;51     var fs = this.fso.OpenTextFile(this.iniFileName, 2, true, -1);52     for (var sec in dic){53       fs.WriteLine(‘[‘ + sec + ‘]‘);54       for (var key in dic[sec]){55         fs.WriteLine(key + ‘=‘ + dic[sec][key]);56       }57     }58     fs.Close();59     fs = null;60   }61 62   IniConfig.prototype.get = function(secName, keyName) {63     var dic = this._iniSecDictionary;64     try{65       return dic[secName][keyName];66     } catch (e) {67       return ‘‘;68     }69   }70 71   IniConfig.prototype.set = function(secName, keyName, val) {72     var dic = this._iniSecDictionary;73     try {74       if (dic[secName] == null) {75         dic[secName] = new Array();76       }77       dic[secName][keyName] = val;78     } catch (e) {79       alert(e.message);80     }81   }82 83   try {84    var iniFile = new IniConfig("E:\\a.ini");85    iniFile.load();86    alert(iniFile.get("Shutdown","0CmdLine"));87    iniFile.set("Shutdown","0CmdLine","aaa");88    iniFile.set("Shutdown","0CmdLine","abc");89    iniFile.set("Shutdown", "1CmdLine", "bbb")90    iniFile.save();91   } catch(e) {92    alert(e.message);93   }

 

JS通過ActiveX讀寫ini設定檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.