已关闭问题

vb6如何创建txt

[ 标签:vb6,创建,txt ]

我想用VB6在D盘里创建个1.txt

问题补充  2009-11-05 20:50
Dir1为什么只能选择C盘的
Ooㄝ界末ㄖoO 回答:5 人气:5 提问时间:2009-11-05 20:37

答案

用FSO对象模型
提问人的追问   2009-11-05 20:38
代码给我
回答人的补充   2009-11-05 20:40

filesystemobject对象不是vb内置对象,使用前必须首先选择[工程]→[引用],在出现的窗口中选择“microsoft scripting runtime”,然后利用filesystemobject的fileexists方法来判断文件是否存在.

Private Sub Form_Load()
Dim Fso As New FileSystemObject
If Fso.FileExists("C:\1.txt") Then '文件路径
MsgBox "文件存在"
Dim FsoText As TextStream
Set FsoText = Fso.OpenTextFile("C:\1.txt", ForWriting, True)
FsoText.WriteLine "写入内容"
Else
MsgBox "文件不存在"
End If
End Sub

提问人的追问   2009-11-05 20:42

那我想在D盘生成一个1.exe

并且把Label1上面的内容输入到1.exe里,这个怎么弄?

并且可以自己选择生成路径
回答人的补充   2009-11-05 20:50
Dim FsoText As TextStream
Set FsoText = Fso.OpenTextFile(路径变量, ForWriting, True)
FsoText.WriteLine Label1.Caption
回答采纳率:16.1% 2009-11-05 20:38
Open "d:\1.txt" For Output As #1
Close #1
提问人的追问   2009-11-05 20:41

那我想在D盘生成一个1.exe

并且把Label1上面的内容输入到1.exe里,这个怎么弄?

提问人的追问   2009-11-05 20:42
并且可以自己选择路径
回答人的补充   2009-11-05 20:42

Open "d:\1.txt" For Output As #1

print #1, label1.caption
Close #1

提问人的追问   2009-11-05 20:43
生成exe
回答人的补充   2009-11-05 20:47

满足全部要求

Private Sub Command1_Click()
Open Text1 & "\1.exe" For Output As #1
Print #1, Label1.Caption
Close #1
End Sub

回答人的补充   2009-11-05 20:57
你用了DIR1哦早说啊 
回答人的补充   2009-11-05 20:58
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
回答人的补充   2009-11-05 21:34

全部代码如下 希望满意:

Private Sub Command1_Click()
Open Text1 & "\1.exe" For Output As #1
Print #1, Label1.Caption
Close #1
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub File1_Click()
Text1 = File1.Path + "\"
End Sub
Private Sub Form_Load()
Command1.Caption = "将1.txt生成在此目录下"
Text1 = "d:\"
End Sub

回答采纳率:22.7% 2009-11-05 20:38

open "d:\1.txt" for append as #1

print #1,"写入内容"

close #1

提问人的追问   2009-11-05 20:45

怎么可以设置成可以自己选择生成路径?

回答人的补充   2009-11-05 20:54

先要在工程-部件里选microsoft common dialog control 6.0对话框

拖出来

对话框名.filter="文本文件(*.txt)|*.txt"

对话框名.showopen

open 对话框名.filename for append as #1

print #1,"写入内容"

close #1

回答采纳率:36.8% 2009-11-05 20:39

Open "d:\1.txt" For Output As #1

Print #1, "创建文件"

Close #1

'

'在d:\1.txt 下写入数据,当 d:\1.txt 不存在时他会自己创建

路径是你自己写的。你可以用绝对路径也可以用app.path相对路径。
回答采纳率:44.2% 2009-11-05 21:06

相关内容