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 Long

Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Function sGetINI(strPath As String, strSection As String, strKey As String, strDefault As String) As String
       Dim strV As String
       Dim aa As Long
       strV = "                                                                                                                                                                                                                                 "
       aa = GetPrivateProfileString(strSection, strKey, strDefault, strV, 255, strPath)
       strV = Trim(strV)
       strV = Left(strV, Len(strV) - 1)
       sGetINI = strV
End Function

Public Sub writeINI(strPath As String, strSection As String, strKey As String, strValue As String)
       WritePrivateProfileString strSection, strKey, strValue, strPath
End Sub

Private 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 Integer

Me.Tag = -1

If App.PrevInstance Then
    Me.Caption = ""        
    hwnd = FindWindow(vbNullString, "RFIDWebServer")        
    If hwnd > 0 Then
        SendMessage hwnd, WM_SYSCOMMAND, SC_RESTORE, 0
        SetForegroundWindow hwnd        
        TopWindow = GetLastActivePopup(hwnd)        
        Unload Me
        Shell_NotifyIcon NIM_DELETE, tray   '删除托盘图标
        End
    End If
 End If
 
On 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 Sub

Private Sub Command1_Click()
Dim commstr As String
Dim delaystr As String
Dim runexefile As String
Dim Retustr
Dim delaym As Integer

runexefile = App.Path & "\RFIDWebServer.exe"
On Error Resume Next

commstr = "SCHTASKS /DELETE /TN RFIDWebServerAutoRun /F "        '删除现有
Retustr = Shell(commstr, 0)

If Check1.value > 0 Then
    commstr = "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, "提示"
Else
    writeINI App.Path & "\SysConfig.ini", "DefaultSetup", "ServerAutoRun", "0"
    MsgBox "已关闭开机自动开启RFIDWebServer网页读写卡功能!", vbInformation + vbOKOnly, "提示"
End If

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vx_13822155058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值