如何用VB新建一个TXT文件在指定的目录下?
如何用VB新建一个TXT文件在指定的目录下?比如说在C:\下建立一个名为IP的txt文件。谢谢了~~~~^-^...
如何用VB新建一个TXT文件在指定的目录下?比如说在C:\下建立一个名为IP的txt文件。
谢谢了~~~~^-^ 展开
谢谢了~~~~^-^ 展开
3个回答
展开全部
也是学vb的阿
1、以下代码可以建立多级文件夹:
Option Explicit
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
'创建指定的目录(可以是多级目录)
Private Function CreateDirectory(ByVal sDirectory As String) As Boolean
'创建指定的目录(可以是多级目录),sDirectory:要创建的文件夹
On Error GoTo ErrHandle
Dim lngResult As Long
sDirectory = checkpath(sDirectory)
lngResult = MakeSureDirectoryPathExists(sDirectory)
CreateDirectory = IIf(lngResult = 0, False, True)
ErrHandle:
If Err <> 0 Then
CreateDirectory = False
End If
End Function
Private Function checkpath(ByVal sPath As String) As String
If Right$(sPath, 1) = "\" Then
checkpath = sPath
Else
checkpath = sPath & "\"
End If
End Function
Private Sub Command1_Click()
CreateDirectory "c:\abc\def\ghi"
End Sub
2~4、主要是文件的“打开”、“读取”、“保存”,多练习一下。下面的仅供参考,实际内容是很多的:
Open "D:\xxx.txt for append As #1
Print #1,"1234"
Print #1,"ABCD"
Close #1
'append 是以追加的方式操作的 还可以换成input,output等等 比如
Open "D:\xxx.txt for input As #1
do while not eof(1)
input #1,a
...
loop
Close #1
'上面的是打开和读取
5、一个判断
If Dir("c:\test.txt") = "" Then
MsgBox "指定文件不存在"
Else
MsgBox "指定文件存在"
End If
1、以下代码可以建立多级文件夹:
Option Explicit
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long
'创建指定的目录(可以是多级目录)
Private Function CreateDirectory(ByVal sDirectory As String) As Boolean
'创建指定的目录(可以是多级目录),sDirectory:要创建的文件夹
On Error GoTo ErrHandle
Dim lngResult As Long
sDirectory = checkpath(sDirectory)
lngResult = MakeSureDirectoryPathExists(sDirectory)
CreateDirectory = IIf(lngResult = 0, False, True)
ErrHandle:
If Err <> 0 Then
CreateDirectory = False
End If
End Function
Private Function checkpath(ByVal sPath As String) As String
If Right$(sPath, 1) = "\" Then
checkpath = sPath
Else
checkpath = sPath & "\"
End If
End Function
Private Sub Command1_Click()
CreateDirectory "c:\abc\def\ghi"
End Sub
2~4、主要是文件的“打开”、“读取”、“保存”,多练习一下。下面的仅供参考,实际内容是很多的:
Open "D:\xxx.txt for append As #1
Print #1,"1234"
Print #1,"ABCD"
Close #1
'append 是以追加的方式操作的 还可以换成input,output等等 比如
Open "D:\xxx.txt for input As #1
do while not eof(1)
input #1,a
...
loop
Close #1
'上面的是打开和读取
5、一个判断
If Dir("c:\test.txt") = "" Then
MsgBox "指定文件不存在"
Else
MsgBox "指定文件存在"
End If
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询