Termos !
Pitäisi saada serveriltä ladattua tietyn kansion koko sisältö...
Kuinkahan tälläinen homma onnistuisi ?
Saan kyllä "listattua" kansion sisällön ja ladattuakin kaikilla (textbox, next) hässäköillä, mutta onko tähän olemassa jotain helpompaa ratkaisua?
No tossa on kumminki koodi jolla irtoo serveriltä tietyn kansion kisältö.
Dim kansio As FtpWebRequest = FtpWebRequest.Create("ftp://Minunservu/Kansio")
kansio.Credentials = New NetworkCredential("Käyttäjänimi", "Salasana")
kansio.Method = WebRequestMethods.Ftp.ListDirectory
Dim sr As New StreamReader(kansio.GetResponse().GetResponseStream())
Dim tiedosto As String = sr.ReadLine()
While Not tiedosto Is Nothing
ListBox1.Items.Add(tiedosto)
tiedosto = sr.ReadLine()
End While
sr.Close()
sr = Nothing
kansio = Nothing
End SubSitte ois vielä kiva saada jonkinlainen info tiedostojen siirrosta esim. Brogressbar.
- Happy -
Moikka taas Happy!
vääntelemällä oheista esimerkkiä saat ehkä aikaan mieleisesi virityksen...
' (väännetty SharpDevelop 4.0:lla)
'Lomakkeelle:
'3 TextBoxia (textBox1, textBox2, textBox3)
'2 ListBoxia (ListBox1, ListBox2)
'2 Nappia (button1, button2),
'1 Labelli (Label1) sekä CheckBoxi (checkBox1)
Imports System.IO
Imports System.Net
Imports System.Text
Public Partial Class MainForm
Private MyFtpRequest As FtpWebRequest = Nothing
Private server As String = String.Empty
Private user As String = String.Empty
Private pass As String = String.Empty
Private MyFolder As String = String.Empty
Private Details As List(Of String) = Nothing
Public Sub New()
Me.InitializeComponent()
End Sub
Sub MainForm_Load(sender As Object, e As EventArgs)
textBox3.PasswordChar = CType("*", Char)
'testamiseen _________________________
textBox1.Text = "ftp_palvelimesi.fi":
textBox2.Text = "tunnuksesi"
textBox3.Text = "salasanasi"
'_____________________________________
textBox1.ReadOnly = True
textBox2.ReadOnly = True
textBox3.ReadOnly = True
ListBox1.Enabled = False
ListBox2.SelectionMode = _
SelectionMode.MultiSimple
ListBox2.Enabled = False
button2.Enabled = False
checkBox1.Visible = False
checkBox1.Enabled = False
MyFolder = Environment.GetFolderPath( _
Environment.SpecialFolder.MyDocuments)
MyFolder += "\ftp_downloads"
Try
MkDir(MyFolder)
Catch ex As Exception
End Try
End Sub
Sub Button1_Click(sender As Object, e As EventArgs)
For i As Integer = 1 To 3
Dim txtbox As TextBox = _
CType(Me.Controls("textBox" & CStr(i)), TextBox)
If txtbox.Text.Trim = String.Empty Then
txtbox.Focus: Exit Sub
End If
txtbox = Nothing
Next
server = "ftp://" + textBox1.Text + "/"
textBox1.ReadOnly = True
user = textBox2.Text: textBox2.ReadOnly = True
pass = textBox3.Text: textBox3.ReadOnly = True
MyFtpRequest = CType(WebRequest. _
Create(server), FtpWebRequest)
MyFtpRequest.Credentials = New NetworkCredential(user, pass)
MyFtpRequest.KeepAlive = False
MyFtpRequest.UseBinary = True
MyFtpRequest.Method = _
WebRequestMethods.Ftp.ListDirectory
Dim MyReader As New StreamReader( _
MyFtpRequest.GetResponse().GetResponseStream())
Dim MyItem As String = MyReader.ReadLine()
ListBox1.Enabled = True
ListBox1.Items.Clear
ListBox1.Items.Add("")
CheckBox1.Text = "Select All"
checkBox1.Checked = False
checkBox1.Visible = False
checkBox1.Enabled = False
While Not MyItem Is Nothing
If MyItem.IndexOf(".") < 0 Then
ListBox1.Items.Add(MyItem)
End if
MyItem = MyReader.ReadLine()
End While
MyReader.Close()
MyReader.Dispose
ListBox1.SelectedIndex = 0
MyFtpRequest = Nothing
End Sub
Sub ListBox1_SelectedIndexChanged( _
sender As Object, e As EventArgs)
ListBox2.Items.Clear
checkBox1.Visible = False
checkBox1.Enabled = False
CheckBox1.Text = "Select All"
Details = Nothing
If ListBox1.SelectedIndex > 0 Then
Dim folder As String = _
ListBox1.SelectedItem.ToString
MyFtpRequest = CType(WebRequest. _
Create(server + folder), FtpWebRequest)
MyFtpRequest.Credentials = _
New NetworkCredential(user, pass)
MyFtpRequest.KeepAlive = False
MyFtpRequest.UseBinary = True
MyFtpRequest.Method = _
WebRequestMethods.Ftp.ListDirectory
Dim MyReader As New StreamReader( _
MyFtpRequest.GetResponse().GetResponseStream())
Dim MyItem As String = MyReader.ReadLine()
ListBox2.Enabled = True
ListBox2.Items.Clear
While Not MyItem Is Nothing
If MyItem.IndexOf(".") > -1 And
Not MyItem.StartsWith(".") Then
ListBox2.Items.Add(MyItem)
End if
MyItem = MyReader.ReadLine()
End While
MyReader.Close()
MyReader.Dispose
MyFtpRequest = Nothing
If listBox2.Items.Count > 0 Then
GetDetails(folder)
checkBox1.Visible = True
checkBox1.Enabled = True
CheckBox1.Text = "Select All"
End If
End If
End Sub
Sub ListBox2_SelectedIndexChanged( _
ender As Object, e As EventArgs)
If listBox2.SelectedItems.Count > 0 Then
button2.Enabled = True
Else
button2.Enabled = False
End If
End Sub
Sub Button2_Click(sender As Object, e As EventArgs)
button1.Enabled = False
button2.Enabled = False
checkBox1.Enabled = False
listBox1.Enabled = False
listBox2.Enabled = False
Dim files As New List(Of String)
For Each Item In listBox2.SelectedItems
files.Add(Item.ToString)
Next
If files.Count > 0 Then
Dim RemoteBasePath As String = server + _
listBox1.SelectedItem.ToString + "/"
Dim RemoteFullPath As String = String.Empty
Dim LocafullPath As String = String.Empty
For i As Integer = 0 To files.Count -1
Dim remoteSize As Long
For j As Integer = 0 To Details.Count -1
If Details(j).IndexOf(files(i)) > -1 Then
Dim pos As Integer = Details(j).IndexOf("www") + 3
Dim hlpStr As String = _
Details(j).Substring(pos, Details(j).Length - pos)
hlpStr = hlpStr.Trim
Dim parts() As String = hlpStr.Split(CType(" ",Char))
remoteSize = CLng(parts(0).Trim)
parts = Nothing
End If
Next j
RemoteFullPath = RemoteBasePath + files.Item(i)
LocafullPath = MyFolder + "\" + files.Item(i)
MyFtpRequest = CType(WebRequest. _
Create(RemoteFullPath), FtpWebRequest)
MyFtpRequest.Credentials = _
New NetworkCredential(user, pass)
MyFtpRequest.KeepAlive = False
MyFtpRequest.UseBinary = True
MyFtpRequest.Method = _
WebRequestMethods.Ftp.DownloadFile
Using FtpResponse As FtpWebResponse = _
CType(MyFtpRequest.GetResponse, FtpWebResponse)
Using ResponseStream As Stream = _
FtpResponse.GetResponseStream
Using fStream As New FileStream( _
LocafullPath, FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do: Application.DoEvents
read = ResponseStream.Read( _
buffer, 0, buffer.Length)
fStream.Write(buffer, 0, read)
Dim bInfo As New FileInfo(LocafullPath)
Label1.Text = "Loading: " + files(i) + _
" " + Cint((Clng(bInfo.Length) * 100) / _
remoteSize).ToString + "%"
bInfo = Nothing
Loop Until read = 0
For j As Integer = 0 To listBox2.Items.Count -1
If listBox2.Items(j) = files.Item(i) Then
listBox2.SetSelected(j, False)
End If
Next j
ResponseStream.Close()
fStream.Flush: fStream.Close
fStream.Dispose: buffer = Nothing
End Using
ResponseStream.Close()
End Using
End Using
MyFtpRequest = Nothing
Next i
End If
files = Nothing
label1.Text = String.Empty
button1.Enabled = True
button2.Enabled = True
checkBox1.Enabled = True
listBox1.Enabled = True
listBox2.Enabled = True
checkBox1.Checked = False
End Sub
Sub GetDetails(folder As String)
Details = New List(Of String)
MyFtpRequest = CType(WebRequest. _
Create(server + folder), FtpWebRequest)
MyFtpRequest.Credentials = New NetworkCredential(user, pass)
MyFtpRequest.KeepAlive = False
MyFtpRequest.UseBinary = True
MyFtpRequest.Method = _
WebRequestMethods.Ftp.ListDirectoryDetails
Dim MyReader As New StreamReader( _
MyFtpRequest.GetResponse().GetResponseStream())
Dim MyItem As String = MyReader.ReadLine()
While Not MyItem Is Nothing
If MyItem.IndexOf(".") > -1 And
Not MyItem.StartsWith(".") Then
Details.Add(MyItem)
End if
MyItem = MyReader.ReadLine()
End While
MyReader.Close()
MyReader.Dispose
MyFtpRequest = Nothing
End Sub
Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
If CheckBox1.Checked Then
For i As Integer = 0 To listBox2.Items.Count -1
listBox2.SetSelected(i,True)
CheckBox1.Text = "Deselect All"
Next
Else
For i As Integer = 0 To listBox2.Items.Count -1
listBox2.SetSelected(i, False)
CheckBox1.Text = "Select All"
Next
End If
End Sub
End ClassMoi.
Tein nyt sitten tälläsen virityksen...
Ensin siis ladataan serveriltä Scorelist.txt tiedosto joka sisältää
ladattavien tiedostojen nimet ja sitte tiedostot ladataa... :)
Imports System.Net
Public Class Form1
Private WithEvents top1 As WebClient
Private Const filenames As String = "C:\ScoreList.txt"
Private Const url As String = "http://Minunservu/Scores/"
Private Const savepath As String = "C:\Scores\"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
top1 = New WebClient
Dim sourceURL1 = "http://Minunservu/ScoreList.txt"
Dim filedir1 = ("C:\ScoreList.txt")
Try
top1.DownloadFileAsync(New Uri(sourceURL1), (filedir1), True)
Catch ex As Exception
MsgBox("Failed " + ErrorToString(), MsgBoxStyle.Critical)
End Try
End Sub
Private Sub top1_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles top1.DownloadFileCompleted
Dim sr As New IO.StreamReader(filenames)
Dim line As String = sr.ReadLine()
Dim req As Net.WebRequest
Dim resp As IO.Stream
Dim out As IO.BinaryWriter
Do While line IsNot Nothing
req = Net.HttpWebRequest.Create(url & line)
resp = req.GetResponse().GetResponseStream()
out = New IO.BinaryWriter(New IO.FileStream(savepath & line, IO.FileMode.OpenOrCreate))
Dim buf(4096) As Byte
Dim k As Int32 = resp.Read(buf, 0, 4096)
Do While k > 0
out.Write(buf, 0, k)
k = resp.Read(buf, 0, 4096)
ProgressBar1.Value += 1
Loop
resp.Close()
out.Close()
line = sr.ReadLine()
Loop
ProgressBar1.Value = 100
ProgressBar1.Visible = False
End Sub
End ClassToimiva paketti mutta, kuinkahan ton itse tiedostojen latauksen saa
Backgroundworkerin hommix ? Kestää toi tiedostojen lataus aikas kauan ja
koko ohjelma "jäätyy" siksi aikaa kun se lataa noita tiedostija"
- Happy -
Laita while loopin sisälle application.doevents()
Kiitti groovyb!!!! :D
Eipä enää ohjelma "jäädy"
- Happy -
Voit myös käyttää säikeitä (threads) monen asian samanaikaiseen suoritukseen.
Aihe on jo aika vanha, joten et voi enää vastata siihen.