『壹』 請問有誰知道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