Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

Sub listdirs(f As Object, nod As Object)

Dim nod2 As Object, fitem As Object, i As Integer


If f Is Nothing Then Exit Sub
For Each fitem In f.Items
If fitem.IsFolder Then
Set nod2 = tv.Nodes.Add(nod, tvwChild, nod.Key & fitem.Name &
pathsep, fitem.Name)
Else ' add file
tv.Nodes.Add nod, tvwChild, nod.Key & fitem.Name, fitem.Name
' listdirs sh.namespace(fitem), nod2
End If
DoEvents
Next
End Sub

Private Sub tv_NodeClick(ByVal Node As MSComctlLib.Node)


If Node.Children > 0 Then Node.Expanded = True: Exit Sub
If Right(Node.Key, 1) = pathsep Then
listdirs sh.NameSpace(Node.Key), Node
Else
MsgBox "this is a file " & node.key
End If
Node.Expanded = True
End Sub

Const FTP_TRANSFER_TYPE_UNKNOWN = &H0


Const FTP_TRANSFER_TYPE_ASCII = &H1
Const FTP_TRANSFER_TYPE_BINARY = &H2
Const INTERNET_DEFAULT_FTP_PORT = 21 ' default for FTP servers
Const INTERNET_SERVICE_FTP = 1
Const INTERNET_FLAG_PASSIVE = &H8000000 ' used for FTP connections
Const INTERNET_OPEN_TYPE_PRECONFIG = 0 ' use registry
configuration
Const INTERNET_OPEN_TYPE_DIRECT = 1 ' direct to net
Const INTERNET_OPEN_TYPE_PROXY = 3 ' via named proxy
Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4 ' prevent using
java/script/INS
Const MAX_PATH = 260
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 Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As
Long
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 FtpSetCurrentDirectory Lib "wininet.dll" Alias
"FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As
String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias
"FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As
String, lpdwCurrentDirectory As Long) As Long
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias
"FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As
Boolean
Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias
"FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As
Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA"
(ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA"
(ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String)
As Boolean
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal
hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String,
ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As
Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal
hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String,
ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias
"InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String,
lpdwBufferLength As Long) As Boolean
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
Const PassiveConnection As Boolean = True
Public hConnection As Long, hOpen As Long, sOrgPath As String, pathsep As String

Public Sub Enumdirs(tv As TreeView, nod As node)


Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
'set the graphics mode to persistent
' Me.AutoRedraw = True
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the first file
hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
'if there's no file, then exit sub
If hFind = 0 Then Exit Sub
'show the filename
' Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0),
vbBinaryCompare) - 1)
Do
'create a buffer
pData.cFileName = String(MAX_PATH, 0)
'find the next file
lRet = InternetFindNextFile(hFind, pData)
'if there's no next file, exit do
If pData.dwFileAttributes And vbDirectory Then
tv.Nodes.Add nod, tvwChild, nod.Key & pathsep & cut(pData.cFileName),
cut(pData.cFileName)
End If
If lRet = 0 Then Exit Do
'show the filename
' Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0),
vbBinaryCompare) - 1)
Loop
'close the search handle
InternetCloseHandle hFind
End Sub
Sub ShowError()
Dim lErr As Long, sErr As String, lenBuf As Long
'get the required buffer size
InternetGetLastResponseInfo lErr, sErr, lenBuf
'create a buffer
sErr = String(lenBuf, 0)
'retrieve the last respons info
InternetGetLastResponseInfo lErr, sErr, lenBuf
'show the last response info
MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
End Sub
Function cut(p As String) As String
cut = Left(p, InStr(p, Chr(0)) - 1)
End Function
Function setdir(hcon As Long, pth As String) As Boolean
setdir = FtpSetCurrentDirectory(hConnection, pth) = 1
End Function
Function getdir()
Dim scurpath As String
scurpath = String(MAX_PATH, 0)
'get the directory
FtpGetCurrentDirectory hConnection, scurpath, Len(scurpath)
getdir = cut(scurpath)
End Function

Function openftp() As Boolean


server =
user =
pass =
'open an internet connection
hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG,
vbNullString, vbNullString, 0)
'connect to the FTP server
hConnection = InternetConnect(hOpen, server, INTERNET_DEFAULT_FTP_PORT, user,
pass, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
'create a buffer to store the original directory
sOrgPath = String(MAX_PATH, 0)
'get the directory
FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
If Len(cut(sOrgPath)) > o Then openftp = True
End Function

Sub closeftp()
'close the FTP connection
InternetCloseHandle hConnection
'close the internet connection
InternetCloseHandle hOpen

End Sub

Private Sub tv_NodeClick(ByVal node As MSComctlLib.node)


If node.Children > 0 Then node.Expanded = True: Exit Sub
'listdirs sh.namespace(node.Key), node
If openftp Then
If setdir(hConnection, node.Key) Then Enumdirs tv, node
closeftp
End If

node.Expanded = True
End Sub

Private Sub filltree()


Dim nod As node, scurdir As String
pathsep = "/"
If openftp Then
scurdir = getdir
Set nod = tv.Nodes.Add(, , scurdir, scurdir)
Enumdirs tv, nod
closeftp
Else: MsgBox "ftp failed to connect"
End If
nod.Expanded = True
End Sub

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Windows.Forms;
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Net.Mime;
using System.Web;
using System.IO.Compression;
using Microsoft.Win32;
using System.Net.Sockets;
using System.Runtime;
namespace TestDummyTWO
{
public partial class Form1 : Form
{
FTPLib.FTP FTPLIB = new FTPLib.FTP();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
/* THIS IS THE FORMULA
* TreeNode MyNodeMain = new TreeNode();
* TreeNode MyNodeChild = new TreeNode();
* MyNodeMain.Text = "Main";
* MyNodeChild.Text = "child";

* mytree.Nodes.Add(MyNodeMain);
* MyNodeMain.Nodes.Add(MyNodeChild);
*/

FTPLIB.server = "******";
FTPLIB.user = "*******";
FTPLIB.pass = "******";
foreach (string S in FTPLIB.ListDirectories())
{
/* Making parent node */
TreeNode ParentNode = new TreeNode();
ParentNode.Text = S.Substring(55);
treeView1.Nodes.Add(ParentNode);
/* Making child node */
FTPLIB.ChangeDir("//" + S.Substring(55) + "/");
foreach (string S2 in FTPLIB.List())
{
FTPLIB.ChangeDir("//" + S.Substring(55) + "/");
TreeNode ChildNode = new TreeNode();
ChildNode.Text = S2.Substring(55);
ParentNode.Nodes.Add(ChildNode);
FTPLIB.ChangeDir(".");
}
}
}
}
}

--------------------------------
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim root As TreeNode = TreeView1.Nodes.Add("Documents")
PopulateTreeView("c:\users\andrew\documents", root)

End Sub
Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
Dim folder As String = String.Empty
Try
'Add the files to treeview
Dim files() As String = IO.Directory.GetFiles(dir)
If files.Length <> 0 Then
Dim fileNode As TreeNode = Nothing
For Each file As String In files
fileNode = parentNode.Nodes.Add(IO.Path.GetFileName(file))
fileNode.Tag = file
Next
End If
'Add folders to treeview
Dim folders() As String = IO.Directory.GetDirectories(dir)
If folders.Length <> 0 Then
Dim folderNode As TreeNode = Nothing
Dim folderName As String = String.Empty
For Each folder In folders
folderName = IO.Path.GetFileName(folder)
folderNode = parentNode.Nodes.Add(folderName)
folderNode.Tag = folder
PopulateTreeView(folder, folderNode)
Next
End If
Catch ex As UnauthorizedAccessException
parentNode.Nodes.Add("Access Denied")
End Try
End Sub
-------------------------------------

Imports System.IO
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Linq
Imports System.Drawing
Imports System.Data

Public Class GPSTestAuto


Private mRootPath() As String = Directory.GetFileSystemEntries("\\192.168.0.35\Test
Drive\ULTS\")
Property RootPath As String
Get
Return mRootPath(0)
End Get
Set(value As String)
mRootPath(0) = value
End Set
End Property
Private Sub GPSTestAuto_Load(sender As System.Object, e As System.EventArgs)
Handles MyBase.Load

Dim mRootNode As New TreeNode


mRootNode.Text = RootPath
mRootNode.Tag = RootPath

mRootNode.Nodes.Add("*DUMMY*")
TreeView1.Nodes.Add(mRootNode)
End Sub
Private Sub TreeView1_BeforeCollapse(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCollapse
' clear the node that is being collapsed
e.Node.Nodes.Clear()
' add a dummy TreeNode to the node being collapsed so it is expandable
e.Node.Nodes.Add("*DUMMY*")
End Sub

Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As


System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
' clear the expanding node so we can re-populate it, or else we end up with
duplicate nodes
e.Node.Nodes.Clear()
' get the directory representing this node
Dim mNodeDirectory As IO.DirectoryInfo
mNodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)
' add each subdirectory from the file system to the expanding node as a
child node
For Each mDirectory As IO.DirectoryInfo In mNodeDirectory.GetDirectories
' declare a child TreeNode for the next subdirectory
Dim mDirectoryNode As New TreeNode
' store the full path to this directory in the child TreeNode's Tag
property
mDirectoryNode.Tag = mDirectory.FullName
' set the child TreeNodes's display text
mDirectoryNode.Text = mDirectory.Name
' add a dummy TreeNode to this child TreeNode to make it expandable
mDirectoryNode.Nodes.Add("*DUMMY*")
' add this child TreeNode to the expanding TreeNode
e.Node.Nodes.Add(mDirectoryNode)
Next
End Sub
End Class

----------------------------------------------------------

Dim folders() As String = IO.Directory.GetDirectories(dir)


underneath is the rest of the code

Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of
IO.DriveInfo) = My.Computer.FileSystem.Drives
Dim rootDir As String = String.Empty
For i As Integer = 0 To drives.Count - 1
If Not drives(i).IsReady Then
Continue For
End If
rootDir = drives(i).Name
TreeView1.Nodes.Add(rootDir)
PopulateTreeView(rootDir, TreeView1.Nodes(i))
Next

End Sub

Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)


Dim folder As String = String.Empty
Try
Dim folders() As String = IO.Directory.GetDirectories(dir)
If folders.Length <> 0 Then
Dim childNode As TreeNode = Nothing
For Each folder In folders
childNode = New TreeNode(folder)
parentNode.Nodes.Add(childNode)
PopulateTreeView(folder, childNode)
Next
End If
Catch ex As UnauthorizedAccessException
parentNode.Nodes.Add(folder & ": Access Denied")
End Try
End Sub

----------------------------------------------------------

You might also like