‘壹’ 请问有谁知道vb2008中如何逐字符读取文本文件
vb2008不太清楚,vb6是这样的:input$(每次读取的字符数,文件号)
每次读取一个字符就是:input(1,文件号)
你自己试一下,看看行不行!!
‘贰’ VB2008 获取指定目录下的文件夹名称
简单,在form1上放一个listbox,一个button,双击button进入代码,贴上:
Dim
sDir
As
String
=
"d:\"
'这里你自己指定
Dim
dDir
As
New
IO.DirectoryInfo(sDir)
Dim
fFileSystemInfo
As
IO.FileSystemInfo
For
Each
fFileSystemInfo
In
dDir.GetFileSystemInfos()
ListBox1.Items.Add(fFileSystemInfo.Name)
Next
F5测试吧
‘叁’ vb2008速成版文件夹问题
如果是英文版就按菜单里的Bulid,如果是中文版按生成,然后在工程目录下找bin。
然后就看你是用什么方式编译的了。如果是Debug方式编译就找 bin\Debug下就有你.exe的运行文件 如果是Release方式编译 就是bin\Release
就这样。
获取数据库路径
数据库连接:
ConnectionString = "provider=SQLOLEDB.1;persist security info=false;user ID=sa;psw=123;initial catalog=人事档案;data suorce=127.0.0.1"
其中你变的地方有3个:
1、user ID=sa中sa为SQL用户名
2、psw=123中123为密码
3、“人事档案”为你的数据库名
补充:一般情况下默认的用户名为sa,而密码是为空的;
所以如果是默认的话(密码为空),这样写就可以了:
ConnectionString = "provider=SQLOLEDB.1;persist security info=false;user ID=sa;initial catalog=人事档案;data suorce=127.0.0.1"
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=temp.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient
‘肆’ vb读取文件夹中所有的txt文件
使用VB内建函数读取文本文件
1/3
双击Command1添加如下代码
Private Sub Command1_Click()
Dim strFile As String
Dim intFile As Integer
Dim strData As String
strFile = "c:\学生成绩.txt"
intFile = FreeFile
Open strFile For Input As intFile
strData = StrConv(InputB(FileLen(strFile), intFile), vbUnicode)
Debug.Print strData
Close intFile
End Sub
2/3
运行代码读取文件
按F8开始单步调试代码,点击Command1,进入单步调试功能,多次按下F8或直接按下F5运行完成,就完成了读取文本文件内容并输出到立即窗口。
查看剩余1张图
3/3
关键代码说明
intFile = FreeFile:获取一个文件句柄
Open strFile For Input As intFile :打开文件
FileLen(strFile) :获取文件内容字节大小
InputB:读取文件内容字节流
StrConv:将字节流转换为Unicode字符串
Debug.Print strData:将字符串内容输出到立即窗口
Close intFile:关闭文件句柄
使用FileSystemObject读取文本文件
1/3
添加Microsoft Scripting Runtime引用
点击Project菜单,再点击下拉菜单中的Reference,打开引用对话框,浏览找到Microsoft Scripting Runtime引用,选择后点确定按钮。
查看剩余1张图
2/3
双击Command2添加如下代码
Private Sub Command2_Click()
Dim objFS
3/3
运行代码读取文件
按F8开始单步调试代码,点击Command2,进入单步调试功能,多次按下F8或直接按下F5运行完成,就完成了读取文本文件内容并输出到立即窗口。
‘伍’ vb 怎样读取一个文件夹里面的指定类型的所有文件的名称
可以实现
可以借助文件列表框来实现
首先加入一个FileListBox 控件,然后加入一下程序
Private Sub Command1_Click()
File1.Path = "C:\Documents and Settings\Administrator\桌面\新建文件夹" '指定一个文件夹
File1.Pattern = "*.txt" '指定文件类型
Dim i As Integer
For i = 0 To File1.ListCount - 1 从列表框的第一个项目找到最后一个项目
Print File1.List(i) '依次输出该文件夹中指定类型的文件名,可以按照自己的需要输出到一个文本文件中,或者自己其他指定
Next i
End Sub
如果需要,可以把该控件的visible属性谁为false,把控件隐藏
‘陆’ vb 如何读取某目录下所有文件及子文件夹
查找某目录下所有 文件 及 子文件夹
试一试不用 FileSystemObject 对象,只用基本控件的代码。
'例子需控件:Command1,List1,List2,File1,Dir1,都采用默认属性。
'例如,查找 C:\ ,带 '** 的语可修改
Dim ctFind As Boolean
Private Sub Form_Load()
Me.Caption = "查找所有文件及文件夹"
Command1.Caption = "查找"
List2.Visible = False: File1.Visible = False: Dir1.Visible = False
Label1.Caption = "就绪"
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub Form_Resize()
Dim W As Long
On Error Resume Next
W = 720
List1.Move 0, 0, Me.ScaleWidth - W - 120, Me.ScaleHeight - 300
Command1.Move Me.ScaleWidth - W - 60, 300, W
Label1.Move 90, Me.ScaleHeight - 255, Screen.Width, 255
End Sub
Private Sub Command1_Click()
ctFind = Not ctFind
If ctFind Then
Command1.Caption = "取消"
Call FindDirFile("C:") '**查找 C:\ 下的所有文件和目录,或 C:\Windows 等
Command1.Caption = "查找"
Else
Command1.Caption = "查找"
End If
End Sub
Private Sub FindDirFile(ByVal nPath As String)
Dim I As Long, nDir As String, Ci As Long
ctFind = True
List1.Clear: List2.Clear
If Right(nPath, 1) <> "\" Then nPath = nPath & "\"
List1.AddItem "查找 " & nPath: List2.AddItem nPath
File1.Pattern = "*"
File1.System = True: File1.Hidden = True: File1.ReadOnly = True
On Error GoTo Cuo
Dir1.Path = nPath
On Error GoTo 0
Do
If List2.ListCount = 0 Then Exit Do
nPath = List2.List(0)
List2.RemoveItem 0
Dir1.Path = nPath
For I = 0 To Dir1.ListCount - 1
GoSub ShowGe
nDir = Dir1.List(I)
If Right(nDir, 1) <> "\" Then nDir = nDir & "\"
List1.AddItem "■" & nDir
List2.AddItem nDir
Next
File1.Path = nPath
For I = 0 To File1.ListCount - 1
GoSub ShowGe
List1.AddItem " " & nPath & File1.List(I)
Next
Loop
Label1.Caption = "查找完毕,共找到 " & List1.ListCount & " 个条目"
ctFind = False
Exit Sub
Cuo:
List1.AddItem "起始目录不存在:" & nPath
ctFind = False
Exit Sub
ShowGe:
Ci = Ci + 1
If Ci < 99 Then Return
Ci = 0
Label1.Caption = "已找到 " & List1.ListCount & " 个:" & nPath
DoEvents
If ctFind Then Return
End Sub
‘柒’ vb怎么打开文件夹
1、点击对话框下菜单,点击打开文件对话框。
‘捌’ VB读取文件夹
据要求:
使用一个filelistbox,三个command。
假设当前文件夹下:
a文件夹,内有1、2、3子文件夹,子文件夹内有若干文件;
b文件夹,内有1、2、3子文件夹,子文件夹内有若干文件;
则使用listcount获取个数,使用IIF来进行比较,不妥之处请自行修改。。。代码见下:
'以下代码利用文本文件保存为Form1.frm运行于VB环境
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command3
Caption = "Command3"
Height = 495
Left = 1680
TabIndex = 3
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command2
Caption = "Command2"
Height = 495
Left = 1680
TabIndex = 2
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 495
Left = 1680
TabIndex = 1
Top = 240
Width = 1215
End
Begin VB.FileListBox File1
Height = 450
Left = 120
TabIndex = 0
Top = 120
Visible = 0 'False
Width = 1215
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim alist As Integer
Private Sub Command1_Click()
File1.Path = App.Path & "\a\1"
alist = File1.ListCount
File1.Path = App.Path & "\b\1"
MsgBox IIf(File1.ListCount = alist, "相同", "不同")
End Sub
Private Sub Command2_Click()
File1.Path = App.Path & "\a\2"
alist = File1.ListCount
File1.Path = App.Path & "\b\2"
MsgBox IIf(File1.ListCount = alist, "相同", "不同")
End Sub
Private Sub Command3_Click()
File1.Path = App.Path & "\a\3"
alist = File1.ListCount
File1.Path = App.Path & "\b\3"
MsgBox IIf(File1.ListCount = alist, "相同", "不同")
End Sub
‘玖’ vb读取文件夹的文件
'来个简单的吧
Private Sub Command1_Click()
Dim objDlg, objF, DstPath, i
Set objDlg = CreateObject("Shell.Application")
Set objF = objDlg.BrowseForFolder(&H0, "选择存放位置:", &H1)
If InStr(1, TypeName(objF), "Folder", vbTextCompare) > 0 Then
DstPath = objF.self.Path
Else
MsgBox "目录无效!"
End If
Set objF = Me.Controls.Add("VB.FilelistBox", "picNew", Me)
With objF ' File1
.Pattern = "*.jpg;*.bmp;*.gif"
.Path = DstPath
For i = 0 To .ListCount - 1
List1.AddItem DstPath & "\" & .List(i) '假设listbox的名称为list1,请根据实际修改
Next
End With
End Sub