yanivpinhas

Hi,

I have website lets say http://www.mywebsite.com/downloads

I want to search for TXT files (*.txt) in this address, and get them to string.

How can I do this how I'm using my user name and password to my web site address

P.s how can I delete them after download them

Best Regards,

Yaniv



Re: Visual Basic Express Edition How to search files in my website ?

ahmedilyas

to get a directory listing, you would need to access it via ftp and request for a directorylisting.

take a look at the FtpWebRequest and FtpWebResponse classes:

http://msdn2.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

There is also code examples in the above on deleting a file from the server as well a downloading a file from the server

http://msdn2.microsoft.com/en-us/library/system.net.ftpwebresponse.aspx

Hope this helps






Re: Visual Basic Express Edition How to search files in my website ?

yanivpinhas

Hi,

I'm begginer in VB2005 and all the examples in the links are for c++ and c# so I not really understand

how to get the list of the files on my website address and how to this in VB2005

Best Regards,

Yaniv





Re: Visual Basic Express Edition How to search files in my website ?

ahmedilyas

Hmm interesting, my apologies - thought it should VB.NET code but guess I was looking at something else.

VB.NET code to delete a file off the server:

public function DeleteFileOnServer(byval serverUri as Uri) as Boolean

  // The serverUri parameter should use the ftp:// scheme.
  // It contains the name of the server file that is to be deleted.
  // Example: ftp://contoso.com/someFile.txt.
  // 
  
  if serverUri.Scheme <> Uri.UriSchemeFtp then  
    return false
  end if
  // Get the object used to communicate with the server.
  Dim request as FtpWebRequest = CType(WebRequest.Create(serverUri), FtpWebRequest)
  request.Method = WebRequestMethods.Ftp.DeleteFile
 
  Dim response as FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
  Console.WriteLine("Delete status: {0}",response.StatusDescription) 
  response.Close()
  return true

end function
 
Downloading data:
 
public function DisplayFileFromServer(byval serverUri as Uri) as Boolean

  // The serverUri parameter should start with the ftp:// scheme.
  if serverUri.Scheme <> Uri.UriSchemeFtp then
  
    return false
  end if
  // Get the object used to communicate with the server.
  Dim request as new WebClient()
  
  // This example assumes the FTP site uses anonymous logon.
  request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com")
  try 
  
    Dim newFileData() as byte = request.DownloadData (serverUri.ToString())
    Dim fileString as string = System.Text.Encoding.UTF8.GetString(newFileData)
    Console.WriteLine(fileString)
  
  catch e as WebException  
    Console.WriteLine(e.ToString())
  end try
  return true
end function





Re: Visual Basic Express Edition How to search files in my website ?

yanivpinhas

I tried the downloads example:

public function DisplayFileFromServer(byval serverUri as Uri) as Boolean
  // The serverUri parameter should start with the ftp:// scheme.
  if serverUri.Scheme <> Uri.UriSchemeFtp then
      return false
  end if
  // Get the object used to communicate with the server.
  Dim request as new WebClient()
    // This example assumes the FTP site uses anonymous logon.
  request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com")
  try 
    Dim newFileData() as byte = request.DownloadData (serverUri.ToString())
    Dim fileString as string = System.Text.Encoding.UTF8.GetString(newFileData)
    Console.WriteLine(fileString)
  catch e as WebException  
    Console.WriteLine(e.ToString())
  end try
  return true
end function
but I didn't know how to call this function
I tried: DisplayFileFromServer(ftp://mywebsite.com)
DisplayFileFromServer("ftp://mywebsite.com")
DisplayFileFromServer('ftp://mywebsite.com')
 
but none of them work!
P.S 
janeDoe@contoso.com - is that the user that logon my website 
to logon to my ftp sever I have user and password, where I put the
password then 
 
 
can you help me please 
 
 
 
 




Re: Visual Basic Express Edition How to search files in my website ?

ahmedilyas

The networkcredentials class is the one that provides/creates credentials, so in here enter your username and password:

request.Credentials = new NetworkCredential("username", "password")

Since the method requires a Uri object, you need to create a Uri object, which will contain your ftp path, and call the method.

Dim myUri as new Uri("ftp://ftp.somesite.com/myDirectory")

if Me.DisplayFileFromServer(myUri) = false then

Console.WriteLine("There was a problem with executing the method")

end if






Re: Visual Basic Express Edition How to search files in my website ?

yanivpinhas

thank you for your help... I did what you say

Public Class Form1

Public Function DisplayFileFromServer(ByVal serverUri As Uri) As Boolean

' The serverUri parameter should start with the ftp:// scheme.

If serverUri.Scheme <> Uri.UriSchemeFtp Then

Return False

End If

' Get the object used to communicate with the server.

Dim request As New Net.WebClient()

' This example assumes the FTP site uses anonymous logon.

' request.Credentials = New Net.NetworkCredential("anonymous", "janeDoe@contoso.com")

request.Credentials = New Net.NetworkCredential("username", "password")

Try

Dim newFileData() As Byte = request.DownloadData(serverUri.ToString())

Dim fileString As String = System.Text.Encoding.UTF8.GetString(newFileData)

MsgBox(fileString)

Catch e As Net.WebException

MsgBox(e.ToString())

End Try

Return True

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim myUri As New Uri("ftp://ftp.mysite.com/downloads")

If Me.DisplayFileFromServer(myUri) = False Then

MsgBox("There was a problem with executing the method")

End If

End Sub

End Class

but I run this program and get msgbox'(I change the Console.WriteLine to msgbox) error 550 from the server

file not a available

I double check the username and the password but still gets the same...

any ideas where the problem here (except me )





Re: Visual Basic Express Edition How to search files in my website ?

ahmedilyas

Just as the error states - the file is not available

You are giving it a directory, a directory is not a file therefore you will get this error. you need to tell it the specific path to the file which you wish to download the contents of.

example:

ftp://ftp.mysite.com/directory/myfile.txt






Re: Visual Basic Express Edition How to search files in my website ?

yanivpinhas

ohhh.. I search for a way to get the names of the files in my website directory

like list of the files *.*

Thought that this will do the job..

Do you know how to do this





Re: Visual Basic Express Edition How to search files in my website ?

ahmedilyas

Sure, you need to request for a Directory listing. To do this, you would need to use the Ftp classes

Code Snippet

private function GetDirectoryListing(byval path as string) as string

Dim request as FtpWebRequest = CType(FtpWebRequest.Create(path), FtpWebRequest)

Dim credential as new NetworkCredential("username", "password")

request.Credentials = credential

request.Method = WebRequestMethods.Ftp.ListDirectory

Dim response as FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)

Dim streamer as Stream = response.GetResponseStream()

Dim reader as new StreamReader(streamer)

return reader.ReadToEnd()

end function

Usage:

Dim dirListing as String = Me.GetDirectoryListing("ftp://ftp.mysite.com/directory")

Be sure to import the System.Net and System.IO namespaces






Re: Visual Basic Express Edition How to search files in my website ?

yanivpinhas

Thank you very much for your help... working great!!

I hope that is ok if I a ask you few more question.. cause you help me a lot

If you can put an upload funcation in the same way it be great

Thanks a lot again... you are great...





Re: Visual Basic Express Edition How to search files in my website ?

ahmedilyas

no problem, glad I could help!

For functionality to upload...you would need to open the file, read the data in a byte() array, then write to the stream which has been connected to the ftp site:

Code Snippet

Dim request As FtpWebRequest = CType(FtpWebRequest.Create("ftp://www.contoso.com/test.htm"),FtpWebRequest)


request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = new NetworkCredential ("username","password")

using sourceStream as new StreamReader("testfile.txt")

Dim fileContents(sourceStream.Length) as Byte = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())

request.ContentLength = fileContents.Length


using requestStream as Stream = request.GetRequestStream()
requestStream.Write(fileContents, 0, fileContents.Length)
end using


Dim response as FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)


Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription)
response.Close()


end using






Re: Visual Basic Express Edition How to search files in my website ?

yanivpinhas

Thanks again...

this line in VB 2005 doesn't work :

Dim fileContents(sourceStream.Length) as Byte = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())

I get 2 errors from the vb (before I tried to bulid and make compilation )about this command:

fileContents(sourceStream.Length)

This are the 2 errors:

filecontents - explicit initialization is not permitted for arrays declared with explicit bounds

sourceStream.Length - 'length' is not a member of 'system.IO.StreamReader'

Can you help with that





Re: Visual Basic Express Edition How to search files in my website ?

ahmedilyas

Sure, my apologies. Try this:

Code Snippet

Dim request As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create("ftp://www.contoso.com/test.htm"), FtpWebRequest)

request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

request.Credentials = New System.Net.NetworkCredential("username", "password")

Using sourceStream As New System.IO.StreamReader("testfile.txt")

Dim fileContents() As Byte = System.Text.Encoding.UTF8.GetBytes(sourceStream.ReadToEnd())

request.ContentLength = fileContents.Length

Using requestStream As System.IO.Stream = request.GetRequestStream()

requestStream.Write(fileContents, 0, fileContents.Length)

End Using

Dim response As System.Net.FtpWebResponse = CType(request.GetResponse(), System.Net.FtpWebResponse)

Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription)

response.Close()

End Using






Re: Visual Basic Express Edition How to search files in my website ?

yanivpinhas

Thanks for your help... everything is working great!!

Best Regards,

Yaniv pinhas