filepath="C:/UserDedinedEnvironment.xml"
Set dics = GetVars(filepath)
addVar filepath,"tttt","aaaa"
updateVar filepath,"tttt","55555555"
delVar filepath,"tttt"
xx=1
Function GetVars(filepath) '已完成
Set objxml= CreateObject("MSXML.DOMDocument")
objxml.load filepath
Set Environ = objxml.lastChild '獲得<Environment>節點
Set dic = CreateObject("Scripting.Dictionary")
allVar = Environ.childNodes.length '獲得<Environment>節點的子節點數量,即變量個數。
For i=0 To allVar-1
Set vars=Environ.childNodes.item(i) '獲得<Variable>節點
var_name=vars.childNodes.item(0).nodeTypedValue
var_value=vars.childNodes.item(1).nodeTypedValue
dic.Add var_name,var_value
Next
Set GetVars = dic '傳回dictionary的環境變量清單
Set dic = Nothing
End Function
Function addVar(filepath,varname,varvalue) '已完成
If dic.Exists(varname) Then
updateVar filepath,varname,varvalue
Exit Function
End If
Set vars =Environ.childNodes.item(0)
Set newVar= vars.cloneNode(1) '複制<Variable>節點
'修改子節點的值
newVar.childNodes.item(0).nodeTypedValue = varname
newVar.childNodes.item(1).nodeTypedValue = varvalue
'添加節點
Set newEnv= Environ.appendChild(newVar)
'儲存xml
objxml.save filepath
Function updateVar(filepath,varname,varvalue) '已完成
' Set dic = CreateObject("Scripting.Dictionary")
' For i=0 To allVar-1
' Set vars=Environ.childNodes.item(i) '獲得<Variable>節點
' var_name=vars.childNodes.item(0).nodeTypedValue
' var_value=vars.childNodes.item(1).nodeTypedValue
' dic.Add var_name,var_value
' Next
' If Not dic.Exists(varname) Then
' addVar filepath,varname,varvalue
' Exit Function
' End If
If var_name=varname Then
vars.childNodes.item(1).nodeTypedValue = varvalue '更新變量值
Exit For
End If
Function delVar(filepath,varname) '已完成
If allVar=1 Then
Environ.removeChild(vars) '删除節點
本文轉自hcy's workbench部落格園部落格,原文連結:http://www.cnblogs.com/alterhu/archive/2012/02/03/2337164.html,如需轉載請自行聯系原作者。