软件制作-----使用VB实现插入系统环境变量,实现一键格式化U盘!
核心源代码代码如下(有点vb基础的一眼就知道,我就不解释了):
VERSION 5.00
Begin VB.Form Form1
Caption = "环境变量"
ClientHeight = 5115
ClientLeft = 120
ClientTop = 450
ClientWidth = 6870
LinkTopic = "Form1"
ScaleHeight = 5115
ScaleWidth = 6870
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text3
Height = 2805
Left = 0
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 5
Text = "Form1.frx":0000
Top = 2250
Width = 6615
End
Begin VB.CommandButton addBtn
Caption = "添加"
Height = 615
Left = 1890
TabIndex = 4
Top = 1560
Width = 1455
End
Begin VB.TextBox Text2
Height = 585
Left = 900
TabIndex = 3
Top = 870
Width = 5715
End
Begin VB.TextBox Text1
Height = 435
Left = 960
TabIndex = 0
Top = 120
Width = 3435
End
Begin VB.Label Label1
Caption = "变量值"
Height = 495
Index = 1
Left = 60
TabIndex = 2
Top = 960
Width = 765
End
Begin VB.Label Label1
Caption = "变量名"
Height = 495
Index = 0
Left = 150
TabIndex = 1
Top = 120
Width = 765
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub addBtn_Click() '
setEnv Trim(Text1.Text), "" ' 先移除
DoEvents
setEnv Trim(Text1.Text), Trim(Text2.Text) '再添加
DoEvents
DoEvents
Text3.Text = getEnviron(Text1.Text)
End Sub
Function isMatch(str, patrn)
Dim objRegExp As Object
Set objRegExp = CreateObject("vbscript.regExp")
objRegExp.Pattern = patrn
isMatch = objRegExp.test(str)
End Function
Private Sub setEnv(ByVal pPath As String, ByVal pValue As String)
On Error GoTo 0
Dim pSysEnv As Object
Set pSysEnv = CreateObject("WScript.Shell").Environment("System")
Dim ExistValueOfPath
If pValue <> "" Then
ExistValueOfPath = pSysEnv(pPath)
If Right(pValue, 1) = "\" Then pValue = Left(pValue, Len(pValue) - 1)
If isMatch(ExistValueOfPath, "\*?" & Replace(pValue, "\", "\\") & "\\?(\b|;)") Then Exit Sub
If ExistValueOfPath <> "" Then pValue = ";" & pValue
pSysEnv(pPath) = ExistValueOfPath & pValue
Else
pSysEnv.Remove (pPath)
End If
End Sub
Function getEnviron(environName As String) As String
Dim x As Integer
Dim Env As String
Dim cmpName() As String
x = 1
Env = Environ(x)
'getEnviron = Env
'Exit Function
Do Until Env = ""
'If InStr(UCase(Env), UCase(environName)) Then
'Me.Caption = Me.Caption & "," & Env
'End If
If InStr(Env, "=") Then
cmpName = Split(Env, "=")
'Debug.Print cmpName(0)
If UCase(Trim(cmpName(0))) = UCase(Trim(environName)) Then
getEnviron = cmpName(1)
Exit Function
End If
End If
x = x + 1
Env = Environ(x)
Loop
getEnviron = ""
End Function
Private Sub Form_Load() 'windir=C:\Windows
'MsgBox getEnviron("Path")
End Sub
Private Sub Text1_Change()
Text3.Text = getEnviron(Trim(Text1.Text))
End Sub
//特殊:
Form1 = 13, 240, 785, 755, , 25, 25, 887, 540, C
ProcessEnvironment = 129, 276, 815, 772, C
SystemEnvironment = 72, 100, 953, 673, C
UserEnvironment = 134, 43, 1021, 616, C
Registry = 35, 106, 768, 679,
效果图如图所示:
软件成品展示:下载地址