Results 1 to 6 of 6

Thread: VS2005 - FTP - Create Directory

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    15

    VS2005 - FTP - Create Directory

    Hi,

    Anyone have any examples about using the FTPWebRequest Method using Visual Studio 2005?

    I've managed to connect to the FTP Server, Get a list of Directories and Navigate to each directory however I'm at a loss as to creating a directory.

    Any help would be appreciated.
    ------------
    - Val -
    ------------

  2. #2
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409

    Re: VS2005 - FTP - Create Directory

    theres an ftp class i found in the MSDN libarary on microsoft.com that i use.


    VB Code:
    1. ' This class permits you to perform direct connections to FTP sites in Visual Basic. NET.
    2. ' The class supports the following FTP commands:
    3. '   - Upload a file
    4. '   - Download a file
    5. '   - Create a directory
    6. '   - Remove a directory
    7. '   - Change directory
    8. '   - Remove a file
    9. '   - Rename a file
    10. '   - Set the user name of the remote user
    11. '   - Set the password of the remote user
    12.  
    13. Imports System
    14. Imports System.Net
    15. Imports System.IO
    16. Imports System.Text
    17. Imports System.Net.Sockets
    18.  
    19.  
    20. 'FTP Class
    21. Public Class clsFTP
    22.  
    23. #Region "Class Variable Declarations"
    24.     Private m_sRemoteHost, m_sRemotePath, m_sRemoteUser As String
    25.     Private m_sRemotePassword, m_sMess As String
    26.     Private m_iRemotePort, m_iBytes As Int32
    27.     Private m_objClientSocket As Socket
    28.  
    29.     Private m_iRetValue As Int32
    30.     Private m_bLoggedIn As Boolean
    31.     Private m_sMes, m_sReply As String
    32.  
    33.     'Set the size of the packet that is used to read and to write data to the FTP server
    34.     'to the following specified size.
    35.     Public Const BLOCK_SIZE = 512
    36.     Private m_aBuffer(BLOCK_SIZE) As Byte
    37.     Private ASCII As Encoding = Encoding.ASCII
    38.     Public flag_bool As Boolean
    39.     'General variable declaration
    40.     Private m_sMessageString As String
    41. #End Region
    42.  
    43. #Region "Class Constructors"
    44.  
    45.     ' Main class constructor
    46.     Public Sub New()
    47.         m_sRemoteHost = "microsoft"
    48.         m_sRemotePath = "."
    49.         m_sRemoteUser = "anonymous"
    50.         m_sRemotePassword = ""
    51.         m_sMessageString = ""
    52.         m_iRemotePort = 21
    53.         m_bLoggedIn = False
    54.     End Sub
    55.  
    56.     ' Parameterized constructor
    57.     Public Sub New(ByVal sRemoteHost As String, _
    58.                    ByVal sRemotePath As String, _
    59.                    ByVal sRemoteUser As String, _
    60.                    ByVal sRemotePassword As String, _
    61.                    ByVal iRemotePort As Int32)
    62.         m_sRemoteHost = sRemoteHost
    63.         m_sRemotePath = sRemotePath
    64.         m_sRemoteUser = sRemoteUser
    65.         m_sRemotePassword = sRemotePassword
    66.         m_sMessageString = ""
    67.         m_iRemotePort = 21
    68.         m_bLoggedIn = False
    69.     End Sub
    70. #End Region
    71.  
    72. #Region "Public Properties"
    73.  
    74.     'Set or Get the name of the FTP server that you want to connect to.
    75.     Public Property RemoteHostFTPServer() As String
    76.         'Get the name of the FTP server.
    77.         Get
    78.             Return m_sRemoteHost
    79.         End Get
    80.         'Set the name of the FTP server.
    81.         Set(ByVal Value As String)
    82.             m_sRemoteHost = Value
    83.         End Set
    84.     End Property
    85.  
    86.     'Set or Get the FTP port number of the FTP server that you want to connect to.
    87.     Public Property RemotePort() As Int32
    88.         'Get the FTP port number.
    89.         Get
    90.             Return m_iRemotePort
    91.         End Get
    92.         'Set the FTP port number.
    93.         Set(ByVal Value As Int32)
    94.             m_iRemotePort = Value
    95.  
    96.         End Set
    97.     End Property
    98.  
    99.     'Set or Get the remote path of the FTP server that you want to connect to.
    100.     Public Property RemotePath() As String
    101.         'Get the remote path.
    102.         Get
    103.             Return m_sRemotePath
    104.         End Get
    105.         'Set the remote path.
    106.         Set(ByVal Value As String)
    107.             m_sRemotePath = Value
    108.         End Set
    109.     End Property
    110.  
    111.     'Set the remote password of the FTP server that you want to connect to.
    112.     Public Property RemotePassword() As String
    113.         Get
    114.             Return m_sRemotePassword
    115.         End Get
    116.         Set(ByVal Value As String)
    117.             m_sRemotePassword = Value
    118.         End Set
    119.     End Property
    120.  
    121.     'Set or Get the remote user of the FTP server that you want to connect to.
    122.     Public Property RemoteUser() As String
    123.         Get
    124.             Return m_sRemoteUser
    125.         End Get
    126.         Set(ByVal Value As String)
    127.             m_sRemoteUser = Value
    128.         End Set
    129.     End Property
    130.  
    131.     'Set the class messagestring.
    132.     Public Property MessageString() As String
    133.         Get
    134.             Return m_sMessageString
    135.         End Get
    136.         Set(ByVal Value As String)
    137.             m_sMessageString = Value
    138.         End Set
    139.     End Property
    140.  
    141. #End Region
    142.  
    143. #Region "Public Subs and Functions"
    144.  
    145.     'Return a list of files from the file system. Return these files in a string() array.
    146.     Public Function GetFileList(ByVal sMask As String) As String()
    147.         Dim cSocket As Socket
    148.         Dim bytes As Int32
    149.         Dim seperator As Char = ControlChars.Lf
    150.         Dim mess() As String
    151.  
    152.         m_sMes = ""
    153.         'Check to see if you are logged on to the FTP server.
    154.         If (Not (m_bLoggedIn)) Then
    155.             Login()
    156.         End If
    157.  
    158.         cSocket = CreateDataSocket()
    159.         'Send an FTP command.
    160.         SendCommand("NLST " & sMask)
    161.  
    162.         If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
    163.             MessageString = m_sReply
    164.             Throw New IOException(m_sReply.Substring(4))
    165.         End If
    166.  
    167.         m_sMes = ""
    168.         Do While (True)
    169.             m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)
    170.             bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
    171.             m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)
    172.  
    173.             If (bytes < m_aBuffer.Length) Then
    174.                 Exit Do
    175.             End If
    176.         Loop
    177.  
    178.         mess = m_sMes.Split(seperator)
    179.         cSocket.Close()
    180.         ReadReply()
    181.  
    182.         If (m_iRetValue <> 226) Then
    183.             MessageString = m_sReply
    184.             Throw New IOException(m_sReply.Substring(4))
    185.         End If
    186.  
    187.         Return mess
    188.     End Function
    189.  
    190.     ' Get the size of the file on the FTP server.
    191.     Public Function GetFileSize(ByVal sFileName As String) As Long
    192.         Dim size As Long
    193.  
    194.         If (Not (m_bLoggedIn)) Then
    195.             Login()
    196.         End If
    197.         'Send an FTP command.
    198.         SendCommand("SIZE " & sFileName)
    199.         size = 0
    200.  
    201.         If (m_iRetValue = 213) Then
    202.             size = Int64.Parse(m_sReply.Substring(4))
    203.         Else
    204.             MessageString = m_sReply
    205.             Throw New IOException(m_sReply.Substring(4))
    206.         End If
    207.  
    208.         Return size
    209.     End Function
    210.  
    211.  
    212.     'Log on to the FTP server.
    213.     Public Function Login() As Boolean
    214.  
    215.         m_objClientSocket = _
    216.         New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    217.  
    218.         Dim ep As New IPEndPoint(Dns.Resolve(m_sRemoteHost).AddressList(0), m_iRemotePort)
    219.  
    220.         Try
    221.             m_objClientSocket.Connect(ep)
    222.         Catch ex As Exception
    223.             MessageString = m_sReply
    224.             Throw New IOException("Cannot connect to the remote server")
    225.  
    226.         End Try
    227.  
    228.         ReadReply()
    229.         If (m_iRetValue <> 220) Then
    230.             CloseConnection()
    231.             MessageString = m_sReply
    232.             Throw New IOException(m_sReply.Substring(4))
    233.         End If
    234.         'Send an FTP command to send a user logon ID to the server.
    235.         SendCommand("USER " & m_sRemoteUser)
    236.         If (Not (m_iRetValue = 331 Or m_iRetValue = 230)) Then
    237.             Cleanup()
    238.             MessageString = m_sReply
    239.             Throw New IOException(m_sReply.Substring(4))
    240.         End If
    241.  
    242.         If (m_iRetValue <> 230) Then
    243.             'Send an FTP command to send a user logon password to the server.
    244.             SendCommand("PASS " & m_sRemotePassword)
    245.             If (Not (m_iRetValue = 230 Or m_iRetValue = 202)) Then
    246.                 Cleanup()
    247.                 MessageString = m_sReply
    248.                 Throw New IOException(m_sReply.Substring(4))
    249.             End If
    250.         End If
    251.  
    252.         m_bLoggedIn = True
    253.         'Call the ChangeDirectory user-defined function to change the directory to the
    254.         'remote FTP folder that is mapped.
    255.         ChangeDirectory(m_sRemotePath)
    256.  
    257.         'Return the final result.
    258.         Return m_bLoggedIn
    259.     End Function
    260.  
    261.     'If the value of mode is true, set binary mode for downloads. Otherwise, set ASCII mode.
    262.     Public Sub SetBinaryMode(ByVal bMode As Boolean)
    263.  
    264.         If (bMode) Then
    265.             'Send the FTP command to set the binary mode.
    266.             '(TYPE is an FTP command that is used to specify representation type.)
    267.             SendCommand("TYPE I")
    268.         Else
    269.             'Send the FTP command to set ASCII mode.
    270.             '(TYPE is an FTP command that is used to specify representation type.)
    271.             SendCommand("TYPE A")
    272.         End If
    273.  
    274.         If (m_iRetValue <> 200) Then
    275.             MessageString = m_sReply
    276.             Throw New IOException(m_sReply.Substring(4))
    277.         End If
    278.     End Sub

    too long for 1 post

  3. #3
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409

    Re: VS2005 - FTP - Create Directory

    VB Code:
    1. ' Download a file to the local directory of the assembly. Keep the same file name.
    2.     Public Sub DownloadFile(ByVal sFileName As String)
    3.         DownloadFile(sFileName, "", False)
    4.     End Sub
    5.     ' Download a remote file to the local directory of the Assembly. Keep the same file name.
    6.     Public Sub DownloadFile(ByVal sFileName As String, _
    7.                             ByVal bResume As Boolean)
    8.         DownloadFile(sFileName, "", bResume)
    9.     End Sub
    10.     'Download a remote file to a local file name. You must include a path.
    11.     'The local file name will be created or will be overwritten, but the path must exist.
    12.     Public Sub DownloadFile(ByVal sFileName As String, _
    13.                             ByVal sLocalFileName As String)
    14.         DownloadFile(sFileName, sLocalFileName, False)
    15.     End Sub
    16.     ' Download a remote file to a local file name. You must include a path. Set the
    17.     ' resume flag. The local file name will be created or will be overwritten, but the path must exist.
    18.     Public Sub DownloadFile(ByVal sFileName As String, _
    19.                             ByVal sLocalFileName As String, _
    20.                             ByVal bResume As Boolean)
    21.         Dim st As Stream
    22.         Dim output As FileStream
    23.         Dim cSocket As Socket
    24.         Dim offset, npos As Long
    25.  
    26.         If (Not (m_bLoggedIn)) Then
    27.             Login()
    28.         End If
    29.  
    30.         SetBinaryMode(True)
    31.  
    32.         If (sLocalFileName.Equals("")) Then
    33.             sLocalFileName = sFileName
    34.         End If
    35.  
    36.         If (Not (File.Exists(sLocalFileName))) Then
    37.             st = File.Create(sLocalFileName)
    38.             st.Close()
    39.         End If
    40.  
    41.         output = New FileStream(sLocalFileName, FileMode.Open)
    42.         cSocket = CreateDataSocket()
    43.         offset = 0
    44.  
    45.         If (bResume) Then
    46.             offset = output.Length
    47.  
    48.             If (offset > 0) Then
    49.                 'Send an FTP command to restart.
    50.                 SendCommand("REST " & offset)
    51.                 If (m_iRetValue <> 350) Then
    52.                     offset = 0
    53.                 End If
    54.             End If
    55.  
    56.             If (offset > 0) Then
    57.                 npos = output.Seek(offset, SeekOrigin.Begin)
    58.             End If
    59.         End If
    60.         'Send an FTP command to retrieve a file.
    61.         SendCommand("RETR " & sFileName)
    62.  
    63.         If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
    64.             MessageString = m_sReply
    65.             Throw New IOException(m_sReply.Substring(4))
    66.         End If
    67.  
    68.         Do While (True)
    69.             m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)
    70.             m_iBytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
    71.             output.Write(m_aBuffer, 0, m_iBytes)
    72.  
    73.             If (m_iBytes <= 0) Then
    74.                 Exit Do
    75.             End If
    76.         Loop
    77.  
    78.         output.Close()
    79.         If (cSocket.Connected) Then
    80.             cSocket.Close()
    81.         End If
    82.  
    83.         ReadReply()
    84.         If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then
    85.             MessageString = m_sReply
    86.             Throw New IOException(m_sReply.Substring(4))
    87.         End If
    88.  
    89.     End Sub
    90.     'This is a function that is used to upload a file from your local hard disk to your FTP site.
    91.     Public Sub UploadFile(ByVal sFileName As String)
    92.         UploadFile(sFileName, False)
    93.     End Sub
    94.     ' This is a function that is used to upload a file from your local hard disk to your FTP site
    95.     ' and then set the resume flag.
    96.     Public Sub UploadFile(ByVal sFileName As String, _
    97.                           ByVal bResume As Boolean)
    98.         Dim cSocket As Socket
    99.         Dim offset As Long
    100.         Dim input As FileStream
    101.         Dim bFileNotFound As Boolean
    102.  
    103.         If (Not (m_bLoggedIn)) Then
    104.             Login()
    105.         End If
    106.  
    107.         cSocket = CreateDataSocket()
    108.         offset = 0
    109.  
    110.         If (bResume) Then
    111.             Try
    112.                 SetBinaryMode(True)
    113.                 offset = GetFileSize(sFileName)
    114.             Catch ex As Exception
    115.                 offset = 0
    116.             End Try
    117.         End If
    118.  
    119.         If (offset > 0) Then
    120.             SendCommand("REST " & offset)
    121.             If (m_iRetValue <> 350) Then
    122.  
    123.                 'The remote server may not support resuming.
    124.                 offset = 0
    125.             End If
    126.         End If
    127.         'Send an FTP command to store a file.
    128.         SendCommand("STOR " & Path.GetFileName(sFileName))
    129.         If (Not (m_iRetValue = 125 Or m_iRetValue = 150)) Then
    130.             MessageString = m_sReply
    131.             Throw New IOException(m_sReply.Substring(4))
    132.         End If
    133.  
    134.         'Check to see if the file exists before the upload.
    135.         bFileNotFound = False
    136.         If (File.Exists(sFileName)) Then
    137.             ' Open the input stream to read the source file.
    138.             input = New FileStream(sFileName, FileMode.Open)
    139.             If (offset <> 0) Then
    140.                 input.Seek(offset, SeekOrigin.Begin)
    141.             End If
    142.  
    143.             'Upload the file.
    144.             m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)
    145.             Do While (m_iBytes > 0)
    146.                 cSocket.Send(m_aBuffer, m_iBytes, 0)
    147.                 m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)
    148.             Loop
    149.             input.Close()
    150.         Else
    151.             bFileNotFound = True
    152.         End If
    153.  
    154.         If (cSocket.Connected) Then
    155.             cSocket.Close()
    156.         End If
    157.  
    158.         'Check the return value if the file was not found.
    159.         If (bFileNotFound) Then
    160.             MessageString = m_sReply
    161.             Throw New IOException("The file: " & sFileName & " was not found. " & _
    162.            "Cannot upload the file to the FTP site")
    163.         End If
    164.  
    165.         ReadReply()
    166.         If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then
    167.             MessageString = m_sReply
    168.             Throw New IOException(m_sReply.Substring(4))
    169.         End If
    170.     End Sub
    171.     ' Delete a file from the remote FTP server.
    172.     Public Function DeleteFile(ByVal sFileName As String) As Boolean
    173.         Dim bResult As Boolean
    174.  
    175.         bResult = True
    176.         If (Not (m_bLoggedIn)) Then
    177.             Login()
    178.         End If
    179.         'Send an FTP command to delete a file.
    180.         SendCommand("DELE " & sFileName)
    181.         If (m_iRetValue <> 250) Then
    182.             bResult = False
    183.             MessageString = m_sReply
    184.         End If
    185.  
    186.         ' Return the final result.
    187.         Return bResult
    188.     End Function
    189.     ' Rename a file on the remote FTP server.
    190.     Public Function RenameFile(ByVal sOldFileName As String, _
    191.                                ByVal sNewFileName As String) As Boolean
    192.         Dim bResult As Boolean
    193.  
    194.         bResult = True
    195.         If (Not (m_bLoggedIn)) Then
    196.             Login()
    197.         End If
    198.         'Send an FTP command to rename from a file.
    199.         SendCommand("RNFR " & sOldFileName)
    200.         If (m_iRetValue <> 350) Then
    201.             MessageString = m_sReply
    202.             Throw New IOException(m_sReply.Substring(4))
    203.         End If
    204.  
    205.         'Send an FTP command to rename a file to a new file name.
    206.         'It will overwrite if newFileName exists.
    207.         SendCommand("RNTO " & sNewFileName)
    208.         If (m_iRetValue <> 250) Then
    209.             MessageString = m_sReply
    210.             Throw New IOException(m_sReply.Substring(4))
    211.         End If
    212.         ' Return the final result.
    213.         Return bResult
    214.     End Function
    way to long

  4. #4
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409

    Re: VS2005 - FTP - Create Directory

    VB Code:
    1. 'This is a function that is used to create a directory on the remote FTP server.
    2.     Public Function CreateDirectory(ByVal sDirName As String) As Boolean
    3.         Dim bResult As Boolean
    4.  
    5.         bResult = True
    6.         If (Not (m_bLoggedIn)) Then
    7.             Login()
    8.         End If
    9.         'Send an FTP command to make directory on the FTP server.
    10.         SendCommand("MKD " & sDirName)
    11.         If (m_iRetValue <> 257) Then
    12.             bResult = False
    13.             MessageString = m_sReply
    14.         End If
    15.  
    16.         ' Return the final result.
    17.         Return bResult
    18.     End Function
    19.     ' This is a function that is used to delete a directory on the remote FTP server.
    20.     Public Function RemoveDirectory(ByVal sDirName As String) As Boolean
    21.         Dim bResult As Boolean
    22.  
    23.         bResult = True
    24.         'Check if logged on to the FTP server
    25.         If (Not (m_bLoggedIn)) Then
    26.             Login()
    27.         End If
    28.         'Send an FTP command to remove directory on the FTP server.
    29.         SendCommand("RMD " & sDirName)
    30.         If (m_iRetValue <> 250) Then
    31.             bResult = False
    32.             MessageString = m_sReply
    33.         End If
    34.  
    35.         ' Return the final result.
    36.         Return bResult
    37.     End Function
    38.     'This is a function that is used to change the current working directory on the remote FTP server.
    39.     Public Function ChangeDirectory(ByVal sDirName As String) As Boolean
    40.         Dim bResult As Boolean
    41.  
    42.         bResult = True
    43.         'Check if you are in the root directory.
    44.         If (sDirName.Equals(".")) Then
    45.             Exit Function
    46.         End If
    47.         'Check if logged on to the FTP server
    48.         If (Not (m_bLoggedIn)) Then
    49.             Login()
    50.         End If
    51.         'Send an FTP command to change directory on the FTP server.
    52.         SendCommand("CWD " & sDirName)
    53.         If (m_iRetValue <> 250) Then
    54.             bResult = False
    55.             MessageString = m_sReply
    56.         End If
    57.  
    58.         Me.m_sRemotePath = sDirName
    59.  
    60.         ' Return the final result.
    61.         Return bResult
    62.     End Function
    63.     ' Close the FTP connection of the remote server.
    64.     Public Sub CloseConnection()
    65.         If (Not (m_objClientSocket Is Nothing)) Then
    66.             'Send an FTP command to end an FTP server system.
    67.             SendCommand("QUIT")
    68.         End If
    69.  
    70.         Cleanup()
    71.     End Sub
    72.  
    73. #End Region
    74.  
    75. #Region "Private Subs and Functions"
    76.     ' Read the reply from the FTP server.
    77.     Private Sub ReadReply()
    78.         m_sMes = ""
    79.         m_sReply = ReadLine()
    80.         m_iRetValue = Int32.Parse(m_sReply.Substring(0, 3))
    81.     End Sub
    82.  
    83.     ' Clean up some variables.
    84.     Private Sub Cleanup()
    85.         If Not (m_objClientSocket Is Nothing) Then
    86.             m_objClientSocket.Close()
    87.             m_objClientSocket = Nothing
    88.         End If
    89.  
    90.         m_bLoggedIn = False
    91.     End Sub
    92.     ' Read a line from the FTP server.
    93.     Private Function ReadLine(Optional ByVal bClearMes As Boolean = False) As String
    94.         Dim seperator As Char = ControlChars.Lf
    95.         Dim mess() As String
    96.  
    97.         If (bClearMes) Then
    98.             m_sMes = ""
    99.         End If
    100.         Do While (True)
    101.             m_aBuffer.Clear(m_aBuffer, 0, BLOCK_SIZE)
    102.             m_iBytes = m_objClientSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
    103.             m_sMes += ASCII.GetString(m_aBuffer, 0, m_iBytes)
    104.             If (m_iBytes < m_aBuffer.Length) Then
    105.                 Exit Do
    106.             End If
    107.         Loop
    108.  
    109.         mess = m_sMes.Split(seperator)
    110.         If (m_sMes.Length > 2) Then
    111.             m_sMes = mess(mess.Length - 2)
    112.         Else
    113.             m_sMes = mess(0)
    114.         End If
    115.  
    116.         If (Not (m_sMes.Substring(3, 1).Equals(" "))) Then
    117.             Return ReadLine(True)
    118.         End If
    119.  
    120.         Return m_sMes
    121.     End Function
    122.     ' This is a function that is used to send a command to the FTP server that you are connected to.
    123.     Private Sub SendCommand(ByVal sCommand As String)
    124.         sCommand = sCommand & ControlChars.CrLf
    125.         Dim cmdbytes As Byte() = ASCII.GetBytes(sCommand)
    126.         m_objClientSocket.Send(cmdbytes, cmdbytes.Length, 0)
    127.         ReadReply()
    128.     End Sub
    129.     ' Create a data socket.
    130.     Private Function CreateDataSocket() As Socket
    131.         Dim index1, index2, len As Int32
    132.         Dim partCount, i, port As Int32
    133.         Dim ipData, buf, ipAddress As String
    134.         Dim parts(6) As Int32
    135.         Dim ch As Char
    136.         Dim s As Socket
    137.         Dim ep As IPEndPoint
    138.         'Send an FTP command to use passive data connection.
    139.         SendCommand("PASV")
    140.         If (m_iRetValue <> 227) Then
    141.             MessageString = m_sReply
    142.             Throw New IOException(m_sReply.Substring(4))
    143.         End If
    144.  
    145.         index1 = m_sReply.IndexOf("(")
    146.         index2 = m_sReply.IndexOf(")")
    147.         ipData = m_sReply.Substring(index1 + 1, index2 - index1 - 1)
    148.  
    149.         len = ipData.Length
    150.         partCount = 0
    151.         buf = ""
    152.  
    153.         For i = 0 To ((len - 1) And partCount <= 6)
    154.             ch = Char.Parse(ipData.Substring(i, 1))
    155.             If (Char.IsDigit(ch)) Then
    156.                 buf += ch
    157.             ElseIf (ch <> ",") Then
    158.                 MessageString = m_sReply
    159.                 Throw New IOException("Malformed PASV reply: " & m_sReply)
    160.             End If
    161.  
    162.             If ((ch = ",") Or (i + 1 = len)) Then
    163.                 Try
    164.                     parts(partCount) = Int32.Parse(buf)
    165.                     partCount += 1
    166.                     buf = ""
    167.                 Catch ex As Exception
    168.                     MessageString = m_sReply
    169.                     Throw New IOException("Malformed PASV reply: " & m_sReply)
    170.                 End Try
    171.             End If
    172.         Next
    173.  
    174.         ipAddress = parts(0) & "." & parts(1) & "." & parts(2) & "." & parts(3)
    175.  
    176.         ' Make this call in Visual Basic .NET 2002. You want to
    177.         ' bitshift the number by 8 bits. In Visual Basic .NET 2002 you must
    178.         ' multiply the number by 2 to the power of 8.
    179.         'port = parts(4) * (2 ^ 8)
    180.  
    181.         ' Make this call and then comment out the previous line for Visual Basic .NET 2003.
    182.         port = parts(4) << 8
    183.  
    184.         ' Determine the data port number.
    185.         port = port + parts(5)
    186.  
    187.         s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    188.         ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0), port)
    189.  
    190.         Try
    191.             s.Connect(ep)
    192.         Catch ex As Exception
    193.             MessageString = m_sReply
    194.             Throw New IOException("Cannot connect to remote server.")
    195.             'If you cannot connect to the FTP server that is
    196.             'specified, make the boolean variable false.
    197.             flag_bool = False
    198.         End Try
    199.         'If you can connect to the FTP server that is specified, make the boolean variable true.
    200.         flag_bool = True
    201.         Return s
    202.     End Function
    203.  
    204. #End Region
    205. End Class

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    15

    Re: VS2005 - FTP - Create Directory

    Thank you for the reply. I previously saw that article on MSDN and was considering it. I however want to use the new objects created in the .NET Framework 2.0. Currently, I can cannect to an FTP Server in the following manner:

    VB Code:
    1. Private Sub DirList(ByVal FTPAddress As String, ByVal UID As String, ByVal PWD As String)
    2.  
    3.         Dim reader As StreamReader = Nothing
    4.         Dim ServerList As String
    5.  
    6.         lblError.Text = ""
    7.  
    8.         Try
    9.             Dim listRequest As FtpWebRequest = WebRequest.Create(FTPAddress)
    10.             listRequest.Credentials = New NetworkCredential(UID, PWD)
    11.             listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails
    12.  
    13.             Dim listResponse As FtpWebResponse = listRequest.GetResponse()
    14.             reader = New StreamReader(listResponse.GetResponseStream())
    15.  
    16.             lstServerList.Items.Clear()
    17.             lstServerList.Items.Add("..")
    18.  
    19.             Do While reader.Peek >= 0
    20.                 ServerList = reader.ReadLine
    21.                 If InStr(1, ServerList, "<DIR>") > 0 Then
    22.                     ServerList = Trim(Mid(ServerList, InStr(1, ServerList, "<DIR>") + 6))
    23.                     lstServerList.Items.Add(ServerList)
    24.                 End If
    25.             Loop
    26.  
    27.             reader.Close()
    28.  
    29.         Catch ex As UriFormatException
    30.             ShowError(ex.tostring, "frmFTPLocation: List(UriFormatException)")
    31.         Catch ex As WebException
    32.             If ex.Message = "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." Then
    33.                 lblError.Text = "No Subdirectories Available"
    34.                 lblError.Visible = True
    35.             Else
    36.                 ShowError(ex.ToString, "frmFTPLocation: List(WebException)")
    37.             End If
    38.         Finally
    39.             If reader IsNot Nothing Then
    40.                 reader.Close()
    41.             End If
    42.         End Try
    43.     End Sub

    I'll incorporate the code you sent me in the mean time so that I can move on in my application. I'll post my complete FTP code using the .NET Framework 2.0 once I get everything running.
    ------------
    - Val -
    ------------

  6. #6
    Hyperactive Member
    Join Date
    Sep 2000
    Location
    NJ, USA
    Posts
    326

    Re: VS2005 - FTP - Create Directory

    Thanks for posting this dogfish. This is a huge help on a progect I'm working on.
    VB.NET 2005 Express with .Net 2.0
    C# 2010 .Net 4.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width