VB如何创建文件夹(路径含有变量);读取文件夹内容(路径含有变量)读取;写入TXT(路径含有变量)

2024-11-28 02:25:29
推荐回答(3个)
回答1:

Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _
Source As Any, ByVal Length As Long)
Private Type Int64
Lo As Long
Hi As Long
End Type

Private Sub cmdSave_Click()
Dim strHex As String, strHex1 As String, userLen As Long, PassLen As String
userLen = Len(Trim(txtUserName.Text))
PassLen = Len(Trim(txtPass.Text))
strHex = NumToHexStr(userLen + PassLen, 2)
strHex = strHex & NumToHexStr(userLen, 2)
strHex = strHex & NumToHexStr(PassLen, 2)
strHex = strHex & Encode(Trim(txtUserName.Text))
strHex = strHex & Encode(Trim(txtPass.Text))
WriteSFText strHex
End Sub

Public Sub WriteSFText(ByVal strHex As String)
Dim FileName As String, Text As String, ProductInfo As String, i As Integer, Sep As String
Dim fso As New FileSystemObject, txt

FileName = App.Path + "\User\UserInfo.dat"
If fso.FileExists(FileName) Then
Set txt = fso.OpenTextFile(FileName, 8, False)
Else
Set txt = fso.CreateTextFile(FileName, True)
End If
txt.WriteLine strHex
txt.Close
Set txt = Nothing
Set fso = Nothing
End Sub

Public Function Encode(strEncode As String) As String
Dim i As Long
Dim chrTmp$
Dim ByteLower$, ByteUpper$
Dim strReturn$ '存储转换后的编码

For i = 1 To Len(strEncode)
chrTmp$ = Mid(strEncode, i, 1)
ByteLower$ = Hex$(AscB(MidB$(chrTmp$, 1, 1)))
If Len(ByteLower$) = 1 Then ByteLower$ = "0" & ByteLower$
ByteUpper$ = Hex$(AscB(MidB$(chrTmp$, 2, 1)))
If Len(ByteUpper$) = 1 Then ByteUpper$ = "0" & ByteUpper$
strReturn$ = strReturn$ & ByteUpper$ & ByteLower$
Next

Encode = strReturn$
End Function

Public Function Decode(strDecode As String) As String
Dim i As Long
Dim strCode$ '存储转换后的编码
Dim chrTmp$

On Error GoTo ErrProc

If Len(strDecode) Mod 4 <> 0 Then GoTo ErrProc
For i = 1 To Len(strDecode) Step 4
strCode = Mid$(strDecode, i, 4)
chrTmp$ = ChrW("&H" & strCode)
If chrTmp$ = "?" Then If strCode <> "003F" Then GoTo ErrProc
Decode = Decode & chrTmp$
Next

Exit Function
ErrProc:
Decode = strDecode
End Function
'*****************************************
'十进制转换为bytLen个字节的十六进制 高低位不转换
'*****************************************
Public Function NumToHexStr(ByVal num As Variant, ByVal bytLen As Integer) As String
Dim str As String
Dim strReturn As String
Dim iLen As Integer
Dim i As Integer

str = BigNumToHexStr(num)
iLen = bytLen * 2
strReturn = ""
If Len(str) >= iLen Then
strReturn = Right(str, iLen)
Else
For i = Len(str) To iLen - 1
strReturn = strReturn & "0"
Next
strReturn = strReturn & str
End If
NumToHexStr = strReturn
End Function
Public Function BigNumToHexStr(ByVal Value As Double) As String
Dim c As Currency
Dim i64 As Int64

c = Value / 10000
CopyMemory i64, c, 8
If i64.Hi Then
BigNumToHexStr = Hex(i64.Hi) & Right("0000000" & Hex(i64.Lo), 8)
Else
BigNumToHexStr = Hex(i64.Lo)
End If
End Function
Public Function HexToDec(ByVal Hex As String) As Double
Dim i As Double
Dim b As Double

Hex = UCase(Hex)
For i = 1 To Len(Hex)
Select Case Mid(Hex, Len(Hex) - i + 1, 1)
Case "0": b = b + 16 ^ (i - 1) * 0
Case "1": b = b + 16 ^ (i - 1) * 1
Case "2": b = b + 16 ^ (i - 1) * 2
Case "3": b = b + 16 ^ (i - 1) * 3
Case "4": b = b + 16 ^ (i - 1) * 4
Case "5": b = b + 16 ^ (i - 1) * 5
Case "6": b = b + 16 ^ (i - 1) * 6
Case "7": b = b + 16 ^ (i - 1) * 7
Case "8": b = b + 16 ^ (i - 1) * 8
Case "9": b = b + 16 ^ (i - 1) * 9
Case "A": b = b + 16 ^ (i - 1) * 10
Case "B": b = b + 16 ^ (i - 1) * 11
Case "C": b = b + 16 ^ (i - 1) * 12
Case "D": b = b + 16 ^ (i - 1) * 13
Case "E": b = b + 16 ^ (i - 1) * 14
Case "F": b = b + 16 ^ (i - 1) * 15
End Select
Next i
HexToDec = b
End Function
Private Sub Form_Load()
'读取用户名密码'
LoadUserName
End Sub
Public Function LoadUserName()
Dim strFileName As String, LineStr As String, userName As String, userNameLen As Long, Pass As String, PassLen As Long, i As Long
strFileName = App.Path + "\User\UserInfo.dat"
Open strFileName For Input As #1
i = 1
Do While Not EOF(1)
Line Input #1, LineStr
userNameLen = HexToDec(Mid(LineStr, 5, 4))
PassLen = HexToDec(Mid(LineStr, 9, 4))
userName = Decode(Mid(LineStr, 13, userNameLen * 4))
Pass = Decode(Mid(LineStr, 13 + userNameLen * 4, PassLen * 4))
i = i + 1
DoEvents
Loop
SubExit:
Close #1
End Function

回答2:

先创建一个目录 再创建这个目录下的子目录 而不是同时
mkdir "c:\test\"
mkdir "c:\test\test123\"

回答3:

没有错误。朋友。变量可以直接引用,无需加小括号的。