* 小心使用 ExpandEnvironmentStrings !!!*

本文介绍了一个关于使用ExpandEnvironmentStrings获取%appdata%路径时可能出现的问题,并提供了解决方案,建议使用SHGetFolderPath来避免由外部程序启动导致的路径获取错误。

最近被一个bug折腾了一下,

用ExpandEnvironmentStrings 获取 %appdata%

正常情况下没问题, 但是当你的程序被外部程序启动时, 可能会产生获取错误的问题(非失败). 比如system给你启动.

 

应该用 SHGetFolderPath  获取.

If Not WScript.Arguments.Named.Exists("elevate") Then CreateObject("Shell.Application").ShellExecute WScript.FullName, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 10 WScript.Quit End If Set oShell = CreateObject("WScript.Shell") Set oEnvSystem = oShell.Environment("SYSTEM") Set oFS = CreateObject("Scripting.FileSystemObject") Dim sBasePath, sJarFile sBasePath = oFS.GetParentFolderName(oFS.GetParentFolderName(WScript.ScriptFullName)) sJarFile = sBasePath & "\ja-netfilter.jar" If Not oFS.FileExists(sJarFile) Then MsgBox "ja-netfilter.jar not found", vbOKOnly Or vbCritical WScript.Quit -1 End If MsgBox "!!!!!!!!!!!请先关闭软件!!!!!!!!!!!" & vbCrLf & vbCrLf & "!!!!!!!!!!!请先关闭软件!!!!!!!!!!!" & vbCrLf & vbCrLf & "!!!!!!!!!!!请先关闭软件!!!!!!!!!!!" & vbCrLf & vbCrLf & "个别电脑执行脚本可能需要十多分钟." & vbCrLf & vbCrLf & "点击确定按钮后,一直等直到'完成'提示的弹出.即可进行下一步操作." & vbCrLf & vbCrLf & "仅用于研究学习 请在使用后24小时内删除." Dim sEnvKey, sEnvVal, aJBProducts aJBProducts = Array("idea", "clion", "phpstorm", "goland", "pycharm", "webstorm", "webide", "rider", "datagrip", "rubymine", "appcode", "dataspell", "gateway", "jetbrains_client", "jetbrainsclient", "studio", "devecostudio") Set re = New RegExp re.Global = True re.IgnoreCase = True re.Pattern = "^\-javaagent:.*[\/\\]ja\-netfilter\.jar.*" Sub RemoveEnv(env) On Error Resume Next For Each sPrd in aJBProducts sEnvKey = UCase(sPrd) & "_VM_OPTIONS" sEnvVal = oShell.ExpandEnvironmentStrings("%" & sEnvKey & "%") If sEnvVal <> ("%" & sEnvKey & "%") Then env.Remove(sEnvKey) End If Next End Sub RemoveEnv oShell.Environment("USER") Dim sVmOptionsFile For Each sPrd in aJBProducts sEnvKey = UCase(sPrd) & "_VM_OPTIONS" sVmOptionsFile = sBasePath & "\vmoptions\" & sPrd & ".vmoptions" If oFS.FileExists(sVmOptionsFile) Then ProcessVmOptions sVmOptionsFile oEnvSystem(sEnvKey) = sVmOptionsFile End If Next Sub ProcessVmOptions(ByVal file) Dim sLine, sNewContent, bMatch Set oFile = oFS.OpenTextFile(file, 1, 0) sNewContent = "" Do Until oFile.AtEndOfStream sLine = oFile.ReadLine bMatch = re.Test(sLine) If Not bMatch Then sNewContent = sNewContent & sLine & vbLf End If Loop oFile.Close sNewContent = sNewContent & "-javaagent:" & sJarFile & "=jetbrains" Set oFile = oFS.OpenTextFile(file, 2, 0) oFile.Write sNewContent oFile.Close End Sub MsgBox "完成." 分析以上脚本功能及安全性
最新发布
09-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值