VB读写ini配置文件将运行文件放入任务计划程序设置为开机自启动
本示例使用设备: https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.52de2c1bWmhJZJ&ft=t&id=562957272162
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPublic Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As LongPublic Function sGetINI(strPath As String, strSection As String, strKey As String, strDefault As String) As StringDim strV As StringDim aa As LongstrV = " "aa = GetPrivateProfileString(strSection, strKey, strDefault, strV, 255, strPath)strV = Trim(strV)strV = Left(strV, Len(strV) - 1)sGetINI = strV
End FunctionPublic Sub writeINI(strPath As String, strSection As String, strKey As String, strValue As String)WritePrivateProfileString strSection, strKey, strValue, strPath
End SubPrivate Sub Form_Initialize()
Dim i As Integer
Dim hwnd As Long
Dim TopWindow As Long
Dim hForeWnd As Long
Dim dwForeID As Long
Dim dwCurID As Long
Dim wParam As Long
Dim lParam As Long
Dim lResult As Long
Dim soundid As IntegerMe.Tag = -1If App.PrevInstance ThenMe.Caption = "" hwnd = FindWindow(vbNullString, "RFIDWebServer") If hwnd > 0 ThenSendMessage hwnd, WM_SYSCOMMAND, SC_RESTORE, 0SetForegroundWindow hwnd TopWindow = GetLastActivePopup(hwnd) Unload MeShell_NotifyIcon NIM_DELETE, tray '删除托盘图标EndEnd IfEnd IfOn Error Resume Next
txtPort.Text = Val(sGetINI(App.Path & "\SysConfig.ini", "DefaultSetup", "WebSocketPort", 39189))
Check3.value = Val(sGetINI(App.Path & "\SysConfig.ini", "DefaultSetup", "MinSystemEn", 0))
Check1.value = Val(sGetINI(App.Path & "\SysConfig.ini", "DefaultSetup", "ServerAutoRun", 0))If sGetINI(App.Path & "\SysConfig.ini", "DefaultSetup", "DebugNetInf", "") = "https://www.icmcu.com" Then: DebugEn = True: Else: DebugEn = False:
End SubPrivate Sub Command1_Click()
Dim commstr As String
Dim delaystr As String
Dim runexefile As String
Dim Retustr
Dim delaym As Integerrunexefile = App.Path & "\RFIDWebServer.exe"
On Error Resume Nextcommstr = "SCHTASKS /DELETE /TN RFIDWebServerAutoRun /F " '删除现有
Retustr = Shell(commstr, 0)If Check1.value > 0 Thencommstr = "SCHTASKS /CREATE /SC MINUTE /MO 2 /TN RFIDWebServerAutoRun /TR " + runexefile '每2分钟运行一次Retustr = Shell(commstr, 0)writeINI App.Path & "\SysConfig.ini", "DefaultSetup", "ServerAutoRun", "1"writeINI App.Path & "\SysConfig.ini", "DefaultSetup", "WebSocketPort", Trim(txtPort.Text)writeINI App.Path & "\SysConfig.ini", "DefaultSetup", "MinSystemEn", Format(Check3.value, "0")MsgBox "已设定开机时自动开启RFIDWebServer网页读写卡功能!", vbInformation + vbOKOnly, "提示"
ElsewriteINI App.Path & "\SysConfig.ini", "DefaultSetup", "ServerAutoRun", "0"MsgBox "已关闭开机自动开启RFIDWebServer网页读写卡功能!", vbInformation + vbOKOnly, "提示"
End IfEnd Sub