VB如何创建文件夹(路径含有变量);读取文件夹内容(路径含有变量)读取;写入TXT(路径含有变量)
我想做一个记账的程序,先注册,密码和名字用用户名保存在TXT文件中再登陆,读取两个文本框,与注册的文件对比,成功后进入FORM2主要问题:如何创建文件夹(路径含有变量)读...
我想做一个记账的程序,
先注册,密码和名字用用户名保存在TXT文件中
再登陆,读取两个文本框,与注册的文件对比,成功后进入FORM2
主要问题:如何创建文件夹(路径含有变量)
读取文件夹内容(路径含有变量)
读取,写入TXT(路径含有变量)
创建隐藏文件夹
在TXT文件中同一行写入汉子和变量
Open "d:\账本保存\save\" & Text1.Text & "_name.txt " For Append As #1
Print #1, Text1.Text
Close #1
MkDir "d:\账本保存\" & (Form1.Text1.Text) & "\" & (Text1.Text) & "\"
Dim strLine As String
Text7.Text = ""
Open "d:\账本保存\" & (Form1.Text1.Text) & "\peoplelist.txt " For Input As #1
While Not EOF(1)
Line Input #1, strLine
Text7.Text = Text7.Text & strLine & vbCrLf
Wend
以上是几端我写的源代码,请多多指教谢谢! 展开
先注册,密码和名字用用户名保存在TXT文件中
再登陆,读取两个文本框,与注册的文件对比,成功后进入FORM2
主要问题:如何创建文件夹(路径含有变量)
读取文件夹内容(路径含有变量)
读取,写入TXT(路径含有变量)
创建隐藏文件夹
在TXT文件中同一行写入汉子和变量
Open "d:\账本保存\save\" & Text1.Text & "_name.txt " For Append As #1
Print #1, Text1.Text
Close #1
MkDir "d:\账本保存\" & (Form1.Text1.Text) & "\" & (Text1.Text) & "\"
Dim strLine As String
Text7.Text = ""
Open "d:\账本保存\" & (Form1.Text1.Text) & "\peoplelist.txt " For Input As #1
While Not EOF(1)
Line Input #1, strLine
Text7.Text = Text7.Text & strLine & vbCrLf
Wend
以上是几端我写的源代码,请多多指教谢谢! 展开
3个回答
展开全部
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
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
追问
你好,我是个新手,您能解释一下我只列出对应的那一个步骤么,谢谢。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先创建一个目录 再创建这个目录下的子目录 而不是同时
mkdir "c:\test\"
mkdir "c:\test\test123\"
mkdir "c:\test\"
mkdir "c:\test\test123\"
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有错误。朋友。变量可以直接引用,无需加小括号的。
追问
但它提示我路径错误啊,能解释一下么,能解释也给您分的,谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询