Kod:
'Dieser Source stammt von http://www.activevb.de
'und kann frei verwendet werden. Für eventuelle Schäden
'wird nicht gehaftet.
'Um Fehler oder Fragen zu klären, nutzen Sie bitte unser Forum.
'Ansonsten viel Spaß und Erfolg mit diesem Source!
Option Explicit
Private Declare Function InternetConnect Lib "wininet.dll" Alias _
"InternetConnectA" (ByVal hInternetSession As Long, _
ByVal sServerName As String, ByVal nServerPort As Integer, _
ByVal sUsername As String, ByVal sPassword As String, _
ByVal lService As Long, ByVal lFlags As Long, ByVal _
lContext As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias _
"InternetOpenA" (ByVal sAgent As String, ByVal lAccessType _
As Long, ByVal sProxyName As String, ByVal sProxyBypass _
As String, ByVal lFlags As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" _
(ByVal hInet As Long) As Integer
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" _
Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As _
Long, ByVal lpszDirectory As String) As Long
Private Declare Function FtpFindFirstFile Lib "wininet.dll" _
Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, _
ByVal lpszSearchFile As String, lpFindFileData As _
WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent _
As Long) As Long
Private Declare Function InternetFindNextFile Lib "wininet.dll" _
Alias "InternetFindNextFileA" (ByVal hFind As Long, _
lpvFindData As WIN32_FIND_DATA) As Long
Private Declare Function FtpGetFile Lib "wininet.dll" Alias _
"FtpGetFileA" (ByVal hFtpSession As Long, ByVal _
lpszRemoteFile As String, ByVal lpszNewFile As String, _
ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes _
As Long, ByVal dwFlags As Long, ByVal dwContext As Long) _
As Long
Private Declare Function FtpPutFile Lib "wininet.dll" Alias _
"FtpPutFileA" (ByVal hFtpSession As Long, ByVal _
lpszLocalFile As String, ByVal lpszRemoteFile As String, _
ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function FtpDeleteFile Lib "wininet.dll" _
Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, _
ByVal lpszFileName As String) As Long
Private Declare Function FtpRenameFile Lib "wininet.dll" _
Alias "FtpRenameFileA" (ByVal hFtpSession As Long, _
ByVal lpszFromFileName As String, ByVal lpszToFileName _
As String) As Long
Private Declare Function FtpCreateDirectory Lib "wininet" _
Alias "FtpCreateDirectoryA" (ByVal hFtpSession As _
Long, ByVal lpszDirectory As String) As Long
Private Declare Function FtpRemoveDirectory Lib "wininet" _
Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As _
Long, ByVal lpszDirectory As String) As Long
Private Declare Function InternetGetLastResponseInfo Lib _
"wininet.dll" Alias "InternetGetLastResponseInfoA" _
(lpdwError As Long, ByVal lpszBuffer As String, _
lpdwBufferLength As Long) As Long
Private Const ERROR_NO_MORE_FILES As Long = 18&
Private Const ERROR_INTERNET_EXTENDED_ERROR As Long = 12003&
Private Const FTP_TRANSFER_TYPE_BINARY As Long = &H0&
Private Const FTP_TRANSFER_TYPE_ASCII As Long = &H1&
Private Const INTERNET_FLAG_PASSIVE As Long = &H8000000
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION As Long = &H400000
Private Const INTERNET_FLAG_MULTIPART As Long = &H200000
Private Const INTERNET_OPEN_TYPE_PRECONFIG As Long = 0&
Private Const INTERNET_OPEN_TYPE_DIRECT As Long = 1&
Private Const INTERNET_OPEN_TYPE_PROXY As Long = 3&
Private Const INTERNET_INVALID_PORT_NUMBER As Long = 0&
Private Const INTERNET_SERVICE_FTP As Long = 1&
Private Const INTERNET_SERVICE_GOPHER As Long = 2&
Private Const INTERNET_SERVICE_HTTP As Long = 3&
Private Declare Function FileTimeToSystemTime Lib "kernel32" _
(lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) _
As Long
Private Const MAX_PATH As Long = 260&
Private Const NO_ERROR As Long = 0&
Private Const FILE_ATTRIBUTE_READONLY As Long = &H1&
Private Const FILE_ATTRIBUTE_HIDDEN As Long = &H2&
Private Const FILE_ATTRIBUTE_SYSTEM As Long = &H4&
Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10&
Private Const FILE_ATTRIBUTE_ARCHIVE As Long = &H20&
Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80&
Private Const FILE_ATTRIBUTE_TEMPORARY As Long = &H100&
Private Const FILE_ATTRIBUTE_COMPRESSED As Long = &H800&
Private Const FILE_ATTRIBUTE_OFFLINE As Long = &H1000&
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Const FTP_UAgent As String = "FTP Demo"
Private FTP_Server As String, FTP_User As String, FTP_PassW As String
Private hOpen As Long, hConnection As Long
Private transfer As Long
Private FileList() As WIN32_FIND_DATA
Private fData As WIN32_FIND_DATA
Private Sub Form_Load()
FTP_Server = "ftp.qualcomm.com"
FTP_User = "anonymous"
FTP_PassW = "guest@unknown"
' FTP_Server = "ftp.adobe.com"
' FTP_User = "anonymous"
' FTP_PassW = "guest@unknown"
Label10.Caption = FTP_Server
Label13.Caption = FTP_User
Label14.Caption = FTP_PassW
Option1(0).Value = True
Call ButtonsDisconnected
Call File1_Click
End Sub
Private Sub Form_Unload(Cancel As Integer)
If hConnection <> 0 Then InternetCloseHandle (hConnection)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
End Sub
Private Sub Command1_Click()
Dim x As Integer
Dim Pfad As String
If File1.ListCount <> 0 Then
For x = 0 To File1.ListCount - 1
If File1.Selected(x) Then
Pfad = File1.Path & "\" & File1.List(x)
If Dir$(Pfad, vbNormal) <> "" Then
Call PutFile(Pfad)
DoEvents
End If
End If
Next x
fListDir (Label1.Caption)
Call FillListBox
End If
End Sub
Private Sub Command2_Click()
Dim fFile As String
Dim x As Integer
x = List1.ListIndex
If x <> -1 Then
For x = 0 To List1.ListCount - 1
If List1.Selected(x) And FileList(x).dwFileAttributes <> _
FILE_ATTRIBUTE_DIRECTORY Then
fFile = Trim$(FileList(x).cFileName)
Call GetFile(Label1.Caption, fFile, File1.Path, Check2.Value = vbChecked)
File1.Refresh
DoEvents
End If
Next x
Else
Call MsgBox("Keine Datei ausgewählt!")
End If
End Sub
Private Sub Command3_Click(Index As Integer)
Select Case Index
Case 0: List1.ListIndex = 1
Call List1_DblClick
Case 1: Call fListDir(Label1.Caption)
Call FillListBox
Case 2: Call ViewFile
Case 3: Call DeleteFile
Case 4: Call RenameFile
Case 5: Call MakeDir
End Select
End Sub
Private Sub Command4_Click()
If hConnection <> 0 Then
Call StatusText("Log off...")
Call InternetCloseHandle(hConnection)
Call InternetCloseHandle(hOpen)
Call GetStatus
hConnection = 0
hOpen = 0
Call ButtonsDisconnected
List1.Clear
Label1.Caption = ""
End If
End Sub
Private Sub Command5_Click()
Dim nFlag As Long
Dim Proxy As String
Dim Er As Boolean
MousePointer = vbHourglass
Label1.Caption = FTP_Server & "/"
Call StatusText("Verbinde...")
'### Für den Proxybetrieb
'Proxy = "...."
'hOpen = InternetOpen(FTP_UAgent, INTERNET_OPEN_TYPE_PROXY, _
' Proxy, vbNullString, 0)
'### Ohne Proxy
hOpen = InternetOpen(FTP_UAgent, INTERNET_OPEN_TYPE_DIRECT, _
vbNullString, vbNullString, 0)
If hOpen <> 0 Then
If Check1.Value Then nFlag = INTERNET_FLAG_PASSIVE
hConnection = InternetConnect(hOpen, Label10.Caption, _
INTERNET_INVALID_PORT_NUMBER, _
Label13.Caption, Label14.Caption, _
INTERNET_SERVICE_FTP, nFlag, 0)
Call GetStatus
If hConnection <> 0 Then
Call fListDir(Label1.Caption)
Call FillListBox
Call ButtonsConnected
Call GetStatus
Else
Er = True
End If
Else
Er = True
End If
If Er Then Call StatusText("Fehler beim Verbindungsaufbau")
MousePointer = vbDefault
End Sub
Private Sub List1_Click()
Dim x As Integer, Attr As Integer
Dim aa As String
Dim l As Long
Dim sTime As SYSTEMTIME
Dim lTime As FILETIME
x = List1.ListIndex
If x > -1 Then
Attr = FileList(x).dwFileAttributes
If Attr And FILE_ATTRIBUTE_READONLY Then aa = "W "
If Attr And FILE_ATTRIBUTE_HIDDEN Then aa = aa & "H "
If Attr And FILE_ATTRIBUTE_SYSTEM Then aa = aa & "S "
If Attr And FILE_ATTRIBUTE_DIRECTORY Then aa = aa & "D "
If Attr And FILE_ATTRIBUTE_ARCHIVE Then aa = aa & "A "
If Attr And FILE_ATTRIBUTE_NORMAL Then aa = aa & "N "
If Attr And FILE_ATTRIBUTE_TEMPORARY Then aa = aa & "T "
If Attr And FILE_ATTRIBUTE_COMPRESSED Then aa = aa & "C"
Label7.Caption = Trim$(aa)
Label12.Caption = FileList(x).nFileSizeLow
lTime = FileList(x).ftLastWriteTime
l = FileTimeToSystemTime(lTime, sTime)
Label15.Caption = CalcFTime(sTime)
End If
End Sub
Private Sub List1_DblClick()
Dim x As Integer
Dim fFile As String, fPath As String
If List1.ListCount <> 0 Then
If FileList(List1.ListIndex).dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY Then
fFile = Trim$(FileList(List1.ListIndex).cFileName)
MousePointer = vbHourglass
If fFile = ".." Then
If Label1.Caption <> FTP_Server & "/" Then
fPath = Label1.Caption & fFile
Call fListDir(fPath)
Call FillListBox
fPath = Left(fPath, Len(fPath) - 3)
For x = Len(fPath) To 1 Step -1
If Mid$(fPath, x, 1) = "/" Then
fPath = Left$(fPath, x)
Exit For
End If
Next x
Label1.Caption = fPath
End If
ElseIf fFile = "." Then
fPath = Label1.Caption
Call fListDir(fPath)
Call FillListBox
Else
fPath = Label1.Caption & fFile
Call fListDir(fPath)
Call FillListBox
Label1.Caption = fPath & "/"
End If
MousePointer = vbDefault
End If
End If
End Sub
Private Sub Option1_Click(Index As Integer)
If Index = 0 Then
transfer = FTP_TRANSFER_TYPE_ASCII
Else
transfer = FTP_TRANSFER_TYPE_BINARY
End If
End Sub
Private Sub File1_Click()
Label5.Caption = File1.Path & "\" & File1.FileName
End Sub
Private Sub Dir1_Change()
File1.Path = Dir1.Path
Label5.Caption = File1.Path & "\" & File1.FileName
End Sub
Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub
Private Sub FillListBox()
Dim fDir() As WIN32_FIND_DATA, fFile() As WIN32_FIND_DATA
Dim Mem As WIN32_FIND_DATA
Dim fName As String, fAttr As String, aa As String
Dim x As Integer, y As Integer, Max As Integer
Max = UBound(FileList) - 1
ReDim fFile(0 To 0)
ReDim fDir(0 To 0)
MousePointer = vbHourglass
Call StatusText("Sortiere Verzeichnis...")
For x = 0 To Max
If FileList(x).dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY Then
fDir(UBound(fDir)) = FileList(x)
ReDim Preserve fDir(0 To UBound(fDir) + 1)
Else
fFile(UBound(fFile)) = FileList(x)
ReDim Preserve fFile(0 To UBound(fFile) + 1)
End If
Next x
For x = 0 To UBound(fDir) - 1
For y = 0 To UBound(fDir) - 1
If LCase(fDir(y).cFileName) > LCase(fDir(x).cFileName) Then
Mem = fDir(y)
fDir(y) = fDir(x)
fDir(x) = Mem
End If
Next y
Next x
For x = 0 To UBound(fFile) - 1
For y = 0 To UBound(fFile) - 1
If LCase(fFile(y).cFileName) > LCase(fFile(x).cFileName) Then
Mem = fFile(y)
fFile(y) = fFile(x)
fFile(x) = Mem
End If
Next y
Next x
List1.Clear
y = 0
For x = 0 To UBound(fDir) - 1
FileList(y) = fDir(x)
List1.AddItem UCase(Trim$(FileList(x).cFileName))
y = y + 1
Next x
For x = 0 To UBound(fFile) - 1
FileList(y) = fFile(x)
List1.AddItem LCase(Trim$(FileList(y).cFileName))
y = y + 1
Next x
MousePointer = vbDefault
Call StatusText("Ok")
End Sub
Private Sub DeleteFile()
Dim fFile As String
Dim x As Integer
Dim Result As Long
x = List1.ListIndex
If x <> -1 Then
If chFDir(Label1.Caption) Then
MousePointer = vbHourglass
fFile = Trim$(FileList(x).cFileName)
If FileList(x).dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY Then
Call StatusText("Lösche Datei...")
Result = FtpDeleteFile(hConnection, fFile)
ElseIf Left$(fFile, 1) <> "." Then
Call StatusText("Lösche Verzeichnis...")
Result = FtpRemoveDirectory(hConnection, fFile)
End If
Call GetStatus
MousePointer = vbDefault
End If
If Result = 0 Then
Call MsgBox("Datei kann nicht gelöscht werden!")
Else
Call fListDir(Label1.Caption)
Call FillListBox
End If
Else
Call MsgBox("Keine Datei ausgewählt!")
End If
End Sub
Private Sub MakeDir()
Dim fDir As String
Dim x As Integer
Dim Result As Long
fDir = Trim$(InputBox("Neuen Verzeichnisnamen eingeben", "Umbenennen"))
If fDir <> "" Then
If chFDir(Label1.Caption) Then
Call StatusText("Erstelle Verzeichnis...")
MousePointer = vbHourglass
Result = FtpCreateDirectory(hConnection, fDir)
MousePointer = vbDefault
Call GetStatus
If Result <> 0 Then
fListDir (Label1.Caption)
Call FillListBox
Else
Call MsgBox("Verzeichnis kann nicht erstellt werden!")
End If
End If
End If
End Sub
Private Sub ViewFile()
Dim fFile As String
Dim x As Integer
x = List1.ListIndex
If x <> -1 Then
If FileList(x).dwFileAttributes <> FILE_ATTRIBUTE_DIRECTORY Then
fFile = Trim$(FileList(x).cFileName)
Call GetFile(Label1.Caption, fFile, App.Path)
Shell "notepad.exe " & App.Path & "\" & fFile, vbNormalFocus
End If
Else
Call MsgBox("Keine Datei ausgewählt!")
End If
End Sub
Private Sub RenameFile()
Dim fFile As String, ToFile As String
Dim x As Integer
Dim Result As Long
x = List1.ListIndex
If x <> -1 Then
fFile = Trim$(FileList(x).cFileName)
ToFile = InputBox("Neuen Dateinamen eingeben", "Umbenennen", fFile)
If Len(ToFile) > 0 Then
If chFDir(Label1.Caption) Then
Call StatusText("Benenne um...")
MousePointer = vbHourglass
Result = FtpRenameFile(hConnection, fFile, ToFile)
Call GetStatus
If Result <> 0 Then
Call fListDir(Label1.Caption)
Call FillListBox
Else
Call MsgBox("Umbenennen nicht möglich")
End If
MousePointer = vbDefault
End If
End If
Else
Call MsgBox("Keine Datei ausgewählt!")
End If
End Sub
Private Sub GetFile(ByVal fDir As String, ByVal fFile As String, _
ByVal lFile As String, ByVal UseCache As Boolean)
Dim Result As Long
Dim Flags As Long
lFile = lFile & "/" & fFile
Call chFDir(fDir)
MousePointer = vbHourglass
StatusText ("Lade Datei...")
If UseCache Then
Flags = transfer
Else
Flags = transfer Or INTERNET_FLAG_RELOAD
End If
Result = FtpGetFile(hConnection, fFile, lFile, 0, 0, Flags, 0)
Call GetStatus
MousePointer = vbDefault
If Result = 0 Then Call MsgBox("Übertragunsfehler")
End Sub
Private Sub PutFile(lFile As String)
Dim Result As Long
Dim x As Integer
Dim fFile As String
If chFDir(Label1.Caption) Then
For x = Len(lFile) To 1 Step -1
If Mid$(lFile, x, 1) = "\" Then
fFile = Mid$(lFile, x + 1)
Exit For
End If
Next x
If fFile <> "" Then
MousePointer = vbHourglass
Call StatusText("Übertrage Datei...")
Result = FtpPutFile(hConnection, lFile, fFile, transfer, 0)
Call GetStatus
MousePointer = vbDefault
If Result = 0 Then Call MsgBox("Übertragungsfehler!")
End If
End If
End Sub
Private Function chFDir(fDir As String) As Boolean
Dim Result As Long
If Len(fDir) > 0 Then
On Error Resume Next
fDir = Mid(fDir, Len(Label10.Caption) + 1, Len(fDir) - Len(Label10.Caption))
On Error GoTo 0
MousePointer = vbHourglass
StatusText ("Setze Pfad...")
Result = FtpSetCurrentDirectory(hConnection, fDir)
Call GetStatus
MousePointer = vbDefault
If Result = 0 Then
Call MsgBox("Verzeichnis nicht vorhanden!")
Else
chFDir = True
End If
End If
End Function
Private Sub fListDir(fDir As String)
Dim hFile As Long, Result As Long
ReDim FileList(0 To 0)
If chFDir(fDir) Then
MousePointer = vbHourglass
Call StatusText("Lese Verzeichnis...")
fData.cFileName = String(MAX_PATH, 0)
hFile = FtpFindFirstFile(hConnection, Trim$(Text1.Text), fData, 0, 0)
Call FileAdd
Do
fData.cFileName = String(MAX_PATH, 0)
Result = InternetFindNextFile(hFile, fData)
If Result <> 0 Then Call FileAdd
Loop Until (Result = 0)
Call InternetCloseHandle(hFile)
Call GetStatus
MousePointer = vbDefault
If Left$(FileList(0).cFileName, 1) <> "." Then
fData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY
fData.cFileName = "." & Chr$(0)
Call FileAdd
fData.cFileName = ".." & Chr$(0)
Call FileAdd
End If
End If
End Sub
Private Sub FileAdd()
Dim x As Integer, fFile As String
fFile = fData.cFileName
x = InStr(1, fFile, Chr$(0))
If x > 0 Then
fFile = Trim$(Left$(fFile, x - 1))
Else
fFile = ""
End If
If fFile <> "" Then
fData.cFileName = fFile
FileList(UBound(FileList)) = fData
ReDim Preserve FileList(0 To UBound(FileList) + 1)
End If
End Sub
Private Function CalcFTime(FTime As SYSTEMTIME) As String
Dim WT(0 To 6) As String, Datum As String
Dim Zeit As String, aa As String
WT(0) = "So"
WT(1) = "Mo"
WT(2) = "Di"
WT(3) = "Mi"
WT(4) = "Do"
WT(5) = "Fr"
WT(6) = "Sa"
With FTime
Datum = WT(.wDayOfWeek) & " " & .wDay & "." & .wMonth & "." & .wYear
aa = .wMinute
If Len(aa) = 1 Then aa = "0" & aa
Zeit = .wHour & ":" & aa
CalcFTime = Datum & " um " & Zeit
End With
End Function
Private Sub GetStatus()
Dim Buffer As String, aa As String
Dim l As Long, Inf As Long
Call InternetGetLastResponseInfo(Inf, vbNullString, l)
If Inf Then
Buffer = String(l + 1, 0)
Call InternetGetLastResponseInfo(Inf, Buffer, l)
aa = Inf & " " & Buffer
Else
aa = "Ok"
End If
Label9.Caption = aa
Label9.Refresh
End Sub
Private Sub StatusText(Text As String)
Label9.Caption = Text
Label9.Refresh
End Sub
Private Sub ButtonsConnected()
Dim x As Integer
Command1.Enabled = True
Command2.Enabled = True
For x = 0 To Command3.UBound
Command3(x).Enabled = True
Next x
Command4.Enabled = True
Command5.Enabled = False
Check1.Enabled = False
End Sub
Private Sub ButtonsDisconnected()
Dim x As Integer
Command1.Enabled = False
Command2.Enabled = False
For x = 0 To Command3.UBound
Command3(x).Enabled = False
Next x
Command4.Enabled = False
Command5.Enabled = True
Check1.Enabled = True
End Sub
umarim isinise yarar....
Bu Konuyu Paylaşın !