Re: FTP CD command




Lou ,

I now understand the problem , i remember to have seen a socket level FTP
client once in the time that the framework did not provide anny FTP
functionality at all , i will see if i can dig it up somewhere .

regards

Michel



"Lou" <lou.garvin@xxxxxxxxxxx> schreef in bericht
news:%23BRhvzIlJHA.4760@xxxxxxxxxxxxxxxxxxxxxxx
Thanks Mike but I am on my third full day of trying to get this done and I
found some stuff from MS that
says what I need to do is not implemented in the Framework(FtpWebRequest)
.
From MS:::sorry.. this is not supported now. We MIGHT have a fully
functional FTP client which maintains state(instead of the stateless
webrequest response model we have now)...in the next release..
Thanks

Heres where it fails.

I ftp into a sever and connect just fine. I am in a directory that's is
deep like
server/dir/dir/dir/dir/dir
I need to explicitly go back NOT forward to the first dir then back down
another dir all with one command.
like I said the VB6 iNet control handles this with one line of code using
the CD command. Simply tell
the server to go directly to a known Directory wherever you are now.

I did see posts to pass the dir in the connect string using escape
characters etc like
("ftp://myFtpUserName:myFtpUserPassword@myFtpUrl/%2E%2E/%2E%2E

Can't get any of that to work with the server.
I realize your app works and I have one very similar but that connects,
you see a dir then can navigate etc.
What I need is the user gives a know directory name then my code need to
just go there and list all the files
in that dir.

Does this make sense
Would love to hear your thoughts and thanks for replying.

-Lou



"Michel Posseth [MCP]" <MSDN@xxxxxxxxxxx> wrote in message
news:eoOhWhElJHA.4344@xxxxxxxxxxxxxxxxxxxxxxx
Huh ,,,,,no i don`t understand it at all

My code connects to a ftp site and the enumerates all content on that
place , if the name does not have a . it asumes this is a directory on
the ftp site if you click on it in the listview it wil go a level deeper
and enumerates all the content in that level and so on and on

So the code is capable of showing the complete directory structure on the
server and interact with it thus Download / Upload ( what more do you
want ? ) , indeed i do not know a way of sending direct commands
although it might be possible with telnet.

regards

Michel




"Lou" <lou.garvin@xxxxxxxxxxx> schreef in bericht
news:eVwXpUElJHA.4696@xxxxxxxxxxxxxxxxxxxxxxx
Thanks but that code doesn't solve my problem. I have the same type of
code working but it can't handle the CD command.
Just passing the dir as the URI doesn't work. eg ftp://server/dir
I have scored the universs and no one has solved this problem. This
seems to be a problem everyone has
run into.
Let me explain.
if i conect to a FTP server using the URI i connect to some unknown
directory. I then want to send a command
to quicky go to some known directory. VB6 iNet contrrol is simple to do
this. .NET it appears impossible.
Some docs say you cann append the dir to the URI but I find that doesn't
work plus you need to know ahead of time
the structure of the directories on the server. With the iNet you simply
give it a know directory name and it just works!!!!

-Louie

"Michel Posseth [MCP]" <MSDN@xxxxxxxxxxx> wrote in message
news:OHt2rvAlJHA.1340@xxxxxxxxxxxxxxxxxxxxxxx

Hello Lou here is my FTP class


<FTP CLASS >
'FTP engine met system.net.webrequest

'Michel Posseth 29-02-2008

'

Imports System.Net

Imports System.IO

Public Class FTP

Public Event eError(ByVal ex As Exception)

#Region "Properties "

Private _Uri As String

''' <summary>

''' Gets or sets the URI voor de FTP site

''' </summary>

''' <value>The URI.</value>

Public Property Uri() As String

Get

Return _Uri

End Get

Set(ByVal value As String)

_Uri = value

End Set

End Property

Private _UserName As String

''' <summary>

''' Gets or sets the name of the user.

''' </summary>

''' <value>The name of the user.</value>

Public Property UserName() As String

Get

Return _UserName

End Get

Set(ByVal value As String)

_UserName = value

End Set

End Property

Private _Password As String

''' <summary>

''' Gets or sets the password.

''' </summary>

''' <value>The password.</value>

Public Property Password() As String

Get

Return _Password

End Get

Set(ByVal value As String)

_Password = value

End Set

End Property

#End Region

#Region " Constructor "

Public Sub New(ByVal Uri As String, Optional ByVal UserName As String =
"", Optional ByVal Password As String = "")

Me.Uri = Uri

Me.UserName = UserName

Me.Password = Password

End Sub

#End Region

Public Event eDirecToryList(ByVal ListItems As String)

Public Sub List(Optional ByVal Details As Boolean = False)

List(Uri, Details)

End Sub

#Region "start stop events "

Public Event eStartFTPMethod(ByVal TaskId As String)

Public Event eStopFTPMethod(ByVal TaskId As String)

#End Region

Public Sub List(ByVal listUrl As String, Optional ByVal Details As
Boolean = False, Optional ByVal Taskid As String = "")

RaiseEvent eStartFTPMethod(Taskid)

Dim reader As StreamReader = Nothing

Try

Dim listRequest As FtpWebRequest = CType(WebRequest.Create(listUrl),
FtpWebRequest)

If String.IsNullOrEmpty(Me.UserName) Then

listRequest.Credentials = New NetworkCredential(Me.UserName,
Me.Password)

End If

If Details Then

listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails

Else

listRequest.Method = WebRequestMethods.Ftp.ListDirectory

End If

Dim listResponse As FtpWebResponse = CType(listRequest.GetResponse(),
FtpWebResponse)

reader = New StreamReader(listResponse.GetResponseStream())

RaiseEvent eDirecToryList(reader.ReadToEnd())

'Catch ex As UriFormatException

'Catch ex As WebException

Catch ex As Exception

RaiseEvent eError(ex)

Finally

If reader IsNot Nothing Then

reader.Close()

End If

RaiseEvent eStopFTPMethod(Taskid)

End Try

End Sub

''' <summary>

''' Uploads the asynch.

''' </summary>

''' <param name="fileName">Name of the file.</param>

''' <param name="uploadUrl">The upload URL.</param>

''' <param name="TaskId">The task id.</param>

Public Sub UploadAsynch(ByVal fileName As String, ByVal uploadUrl As
String, Optional ByVal TaskId As String = "")

If String.IsNullOrEmpty(TaskId) Then

TaskId = Guid.NewGuid.ToString

End If

Dim AsyncUpload As New InvokeThreeStringMeth(AddressOf Me.Upload)

AsyncUpload.BeginInvoke(fileName, uploadUrl, TaskId, Nothing, Nothing)

End Sub

''' <summary>

''' Uploads the specified file name.

''' </summary>

''' <param name="fileName">Name of the file.</param>

''' <param name="uploadUrl">The upload URL.</param>

''' <param name="TaskId">The task id.</param>

Public Sub Upload(ByVal fileName As String, ByVal uploadUrl As String,
Optional ByVal TaskId As String = "")

RaiseEvent eStartFTPMethod(TaskId)

Dim requestStream As Stream = Nothing

Dim fileStream As FileStream = Nothing

Dim uploadResponse As FtpWebResponse = Nothing

Try

Dim uploadRequest As FtpWebRequest =
CType(WebRequest.Create(uploadUrl), FtpWebRequest)

If String.IsNullOrEmpty(Me.UserName) Then

uploadRequest.Credentials = New NetworkCredential(Me.UserName,
Me.Password)

End If

uploadRequest.Method = WebRequestMethods.Ftp.UploadFile

'disable eventuele proxy ( uploaden door proxy is niet ondersteund )

uploadRequest.Proxy = Nothing

requestStream = uploadRequest.GetRequestStream()

fileStream = File.Open(fileName, FileMode.Open)

Dim buffer(1024) As Byte

Dim bytesRead As Integer

While True

bytesRead = fileStream.Read(buffer, 0, buffer.Length)

If bytesRead = 0 Then

Exit While

End If

requestStream.Write(buffer, 0, bytesRead)

End While

requestStream.Close()

uploadResponse = CType(uploadRequest.GetResponse(), FtpWebResponse)

Catch ex As Exception

RaiseEvent eError(ex)

'Catch ex As UriFormatException

'

'Catch ex As IOException

'

'Catch ex As WebException

'

Finally

If uploadResponse IsNot Nothing Then

uploadResponse.Close()

End If

If fileStream IsNot Nothing Then

fileStream.Close()

End If

If requestStream IsNot Nothing Then

requestStream.Close()

End If

RaiseEvent eStopFTPMethod(TaskId)

End Try

End Sub

Delegate Sub InvokeThreeStringMeth(ByVal ParA As String, ByVal ParB As
String, ByVal ParC As String)

''' <summary>

''' Download een file asynchroon

''' </summary>

''' <param name="downloadUrl">The download URL.</param>

''' <param name="TargetLocation">The target location.</param>

''' <param name="TaskId">The task id.</param>

Public Sub DownLoadAsynch(ByVal downloadUrl As String, Optional ByVal
TargetLocation As String = "", Optional ByVal TaskId As String = "")

If String.IsNullOrEmpty(TaskId) Then

TaskId = Guid.NewGuid.ToString

End If

Dim AsyncDownLoad As New InvokeThreeStringMeth(AddressOf Me.Download)

AsyncDownLoad.BeginInvoke(downloadUrl, TargetLocation, TaskId, Nothing,
Nothing)

End Sub

''' <summary>

''' Download een file synchroon

'''

''' </summary>

''' <param name="downloadUrl">The download URL.</param>

''' <param name="TargetLocation">The target location.</param>

''' <param name="TaskId">The task id.</param>

Public Sub Download(ByVal downloadUrl As String, Optional ByVal
TargetLocation As String = "", Optional ByVal TaskId As String = "")

RaiseEvent eStartFTPMethod(TaskId)

Dim responseStream As Stream = Nothing

Dim fileStream As FileStream = Nothing

Dim reader As StreamReader = Nothing

Try

Dim downloadRequest As FtpWebRequest = _

CType(WebRequest.Create(downloadUrl), FtpWebRequest)

Dim downloadResponse As FtpWebResponse = _

CType(downloadRequest.GetResponse(), FtpWebResponse)

responseStream = downloadResponse.GetResponseStream()

Dim fileName As String = _

Path.GetFileName(downloadRequest.RequestUri.AbsolutePath)

If fileName.Length = 0 Then

reader = New StreamReader(responseStream)

Else

Dim IOPath As String = fileName

'als we geen targetpath hebben dan gaat de file naar de assembly
directory

If Not String.IsNullOrEmpty(TargetLocation) AndAlso
Directory.Exists(TargetLocation) Then

IOPath = Path.Combine(TargetLocation, fileName)

End If

fileStream = File.Create(IOPath) 'als de file al bestaat wordt deze
overschreven

Dim buffer(1024) As Byte

Dim bytesRead As Integer

While True

'vul de buffer met bytes

bytesRead = responseStream.Read(buffer, 0, buffer.Length)

If bytesRead = 0 Then

'als we geen bytes meer krijgen uit de stream dan is de file compleet
over

Exit While 'dus stoppen met de loop

End If

fileStream.Write(buffer, 0, bytesRead)

End While

'de finally zorgt voor het netjes afsluiten van de stream en file

End If

Catch ex As Exception

RaiseEvent eError(ex)

'Catch ex As UriFormatException

'

'Catch ex As WebException

'

'Catch ex As IOException

'

Finally

If reader IsNot Nothing Then

reader.Close()

ElseIf responseStream IsNot Nothing Then

responseStream.Close()

End If

If fileStream IsNot Nothing Then

fileStream.Close()

End If

RaiseEvent eStopFTPMethod(TaskId)

End Try

End Sub

End Class

</FTP CLASS>



And here is i test application drag and drop a new form in the ide

now drag a textbox , a button and a listbox and a folderbrowserdialog

on the form go to code view and copy paste this code

< TEST APP >

Imports System.Net

Imports System.IO

Imports ista.FTP

Public Class Form1

'' Dim Uri As String = "ftp://nl_sdm1_r23/";

Delegate Sub ParA(ByVal parA As String)

Delegate Sub ParB(ByVal parB As Exception)

Private WithEvents ftpmp As ista.FTP

Private Sub ftpmp_eDirecToryList(ByVal ListItems As String) Handles
ftpmp.eDirecToryList

Me.ListBox1.Items.Clear()

Me.ListBox1.BeginUpdate()

For Each item In ListItems.Split(CChar(vbCrLf))

If item.Trim.Length > 0 Then

Me.ListBox1.Items.Add(item)

End If

Next

Me.ListBox1.EndUpdate()

End Sub

Private Sub ftpmp_eError(ByVal ex As System.Exception) Handles
ftpmp.eError

MsgBox(ex.ToString)

End Sub

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Navigate()

End Sub

Private Sub Navigate()

Button1.Enabled = False

ftpmp = New ista.FTP(Me.TextBox1.Text)

ftpmp.List()

Button1.Enabled = True

End Sub

Private Sub ftpmp_eStartFTPMethod(ByVal TaskId As String) Handles
ftpmp.eStartFTPMethod

If Me.InvokeRequired Then

Dim d As New ParA(AddressOf Me.ftpmp_eStartFTPMethod)

Me.Invoke(d, New Object() {TaskId})

Else

Me.Cursor = Cursors.WaitCursor

End If

End Sub

Private Sub ftpmp_eStopFTPMethod(ByVal TaskId As String) Handles
ftpmp.eStopFTPMethod

If Me.InvokeRequired Then

Dim d As New ParA(AddressOf Me.ftpmp_eStopFTPMethod)

Me.Invoke(d, New Object() {TaskId})

Else

Me.Cursor = Cursors.Default

End If

End Sub

Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.DoubleClick

If Not ListBox1.SelectedItem Is Nothing Then

If ListBox1.SelectedItem.ToString.Length > 1 Then

If ListBox1.SelectedItem.ToString.Contains(".") Then

'een file

If Me.FolderBrowserDialog1.ShowDialog(Me) =
Windows.Forms.DialogResult.OK AndAlso
Me.FolderBrowserDialog1.SelectedPath.Length > 0 Then

ftpmp.DownLoadAsynch(Path.Combine(Me.TextBox1.Text,
ListBox1.SelectedItem.ToString.Trim),
Me.FolderBrowserDialog1.SelectedPath)



End If



Else

'een subdirectory

Me.TextBox1.Text = Path.Combine(Me.TextBox1.Text,
ListBox1.SelectedItem.ToString)

Navigate()

End If

End If

End If

End Sub

End Class


</TEST APP>

note that my apps are all standard contained in the ista root namespace
you might change that to your own

for the rest it should work out of the box


ofcourse uploading is as easy as

Dim FTpUpl As New ista.FTP("ftp://nl_sdm1_r40/";)

FTpUpl.Upload("C:\micheltest.txt",
"ftp://nl_sdm1_r40/XMLOL/test/hallo.txt";)

MsgBox("klaar")



HTH

Michel





"Lou" <lou.garvin@xxxxxxxxxxx> schreef in bericht
news:edwG0h9kJHA.3696@xxxxxxxxxxxxxxxxxxxxxxx
I need to FTP to a server and give it a directory name. I never know
where the directory is
in relation to the root director so I have to do a "CD Dir"
The VB6 iNet control works like a charm but I can't for the life of me
figure out how this is done
in .NET. I scoured Google and found a ton a folks with the same issue
so this is my last hope.

VB6 iNet control code
iNet1..Execute , "CD " & myDir

works like a charm, MUST be a .NET equivalent for something so simple
and widely used....I Hope.
I did find an "Execute" method in FTP WebRequest but it doesn't except
the "CD" command as a string parameter.

-Lou











.


Loading