Avatar billede MadsHaupt Juniormester
02. april 2013 - 13:20 Der er 1 kommentar og
1 løsning

Klasse problem

Jeg har lavet en klasse(FtpDirectoryInfo) til at finde filer og mapper på en ftp server men GetDirectories funktionen virker ikke fordi den laver en array af FtpDirectoryInfo så være gang jeg setter et index så får Me den samme værdi, Hvordan undgår jeg det?.



Code:

Imports System.IO
Imports System.Net

Public Class FtpFileInfo

    Private Shared Username_, Password_, FullName_ As String

    Public Sub New(ByVal Path As String, ByVal Username As String, ByVal Password As String)
        FullName_ = Path
        Username_ = Username
        Password_ = Password
    End Sub

    Public ReadOnly Property FullName As String
        Get
            Return FullName_
        End Get
    End Property
    Public ReadOnly Property Name As String
        Get
            Dim split() As String = FullName_.Split("/")
            Return split(split.Length - 1)
        End Get
    End Property
    Public ReadOnly Property Directory As FtpDirectoryInfo
        Get
            Return New FtpDirectoryInfo(Split(FullName_, "/" & Name)(0), Username_, Password_)
        End Get
    End Property
    Public Function GetStream() As Stream
        Dim a As FtpWebRequest = FtpWebRequest.Create(FullName)
        a.Credentials = New NetworkCredential(Username_, Password_)
        a.Method = WebRequestMethods.Ftp.DownloadFile
        Return a.GetResponse.GetResponseStream
    End Function
    Public Overrides Function ToString() As String
        Return FullName_
    End Function
End Class
Public Class FtpDirectoryInfo
    Private Shared Username_, Password_, FullName_ As String

    Private Class ty
        Public Shared Function GetNewFtpDirectoryInfo(ByVal Path As String, ByVal Username As String, ByVal Password As String)
            Return New FtpDirectoryInfo(Path, Username, Password)
        End Function
    End Class

    Public Sub New(ByVal Path As String, ByVal Username As String, ByVal Password As String)
        FullName_ = Path
        Username_ = Username
        Password_ = Password
    End Sub

    Public ReadOnly Property FullName As String
        Get
            Return FullName_
        End Get
    End Property
    Public ReadOnly Property Name As String
        Get
            Dim split() As String = FullName_.Split("/")
            Return split(split.Length - 1)
        End Get
    End Property
    Public ReadOnly Property Directory As FtpDirectoryInfo
        Get
            Return New FtpDirectoryInfo(Split(FullName_, "/" & Name)(0), Username_, Password_)
        End Get
    End Property
    Public Function GetDirectories() As FtpDirectoryInfo()
        Dim a As FtpWebRequest = FtpWebRequest.Create("ftp://ftp.madshaupt.dk/")
        a.Credentials = New NetworkCredential(Username_, Password_)
        a.Method = WebRequestMethods.Ftp.ListDirectoryDetails
        Dim strs() As String = New StreamReader(a.GetResponse.GetResponseStream).ReadToEnd.Split(vbNewLine)
        Dim dirslength As Integer = 0
        For i = 0 To strs.Length - 1
            If Split(strs(i), "drwxr-xr-x").Length = 2 Then
                dirslength += 1
            End If
        Next
        Dim dirs(dirslength) As FtpDirectoryInfo
        Dim i2 As Integer = 0
        For i = 0 To strs.Length - 1
            If Split(strs(i), "drwxr-xr-x").Length = 2 Then
                dirs(i2) = New FtpDirectoryInfo(FullName_ & strs(i).Split(":")(1).Substring(3), Username_, Password_)
                i2 += 1
            End If
        Next
        Return dirs
    End Function
    Public Function GetFiles() As FtpFileInfo()
        Dim a As FtpWebRequest = FtpWebRequest.Create("ftp://ftp.madshaupt.dk/")
        a.Credentials = New NetworkCredential(Username_, Password_)
        a.Method = WebRequestMethods.Ftp.ListDirectoryDetails
        Dim strs() As String = New StreamReader(a.GetResponse.GetResponseStream).ReadToEnd.Split(vbNewLine)
        Dim fileslength As Integer = 0
        For i = 0 To strs.Length - 1
            If Split(strs(i), "-rwxr-xr-x").Length = 2 Then
                fileslength += 1
            End If
        Next
        Dim files(fileslength) As FtpFileInfo
        Dim i2 As Integer = 0
        For i = 0 To strs.Length - 1
            If Split(strs(i), "-rwxr-xr-x").Length = 2 Then
                i2 += 1
                Dim Str As String = strs(i).Split(":")(1)
                files(i2) = New FtpFileInfo(FullName_ & Str.Substring(2, Str.Length - 2).Substring(1, Str.Length - 3) & "/", Username_, Password_)
            End If
        Next
        Return files
    End Function
    Public Shared Function GetFiles(ByVal Ftp As String, ByVal FileDirInfo As String, ByVal Username As String, ByVal Password As String) As FtpFileInfo()
        Dim strs() As String = FileDirInfo.Split(vbNewLine)
        Dim fileslength As Integer = 0
        For i = 0 To strs.Length - 1
            If Split(strs(i), "-rwxr-xr-x").Length = 2 Then
                fileslength += 1
            End If
        Next
        Dim files(fileslength) As FtpFileInfo
        Dim i2 As Integer = 0
        For i = 0 To strs.Length - 1
            If Split(strs(i), "-rwxr-xr-x").Length = 2 Then
                i2 += 1
                Dim Str As String = strs(i).Split(":")(1)
                files(i2) = New FtpFileInfo(Ftp & Str.Substring(2, Str.Length - 2).Substring(1, Str.Length - 3) & "/", Username, Password)
            End If
        Next
        Return files
    End Function
    Public Shared Function GetDirectories(ByVal Ftp As String, ByVal FileDirInfo As String, ByVal Username As String, ByVal Password As String) As FtpDirectoryInfo()
        Dim strs() As String = FileDirInfo.Split(vbNewLine)
        Dim dirslength As Integer = 0
        For i = 0 To strs.Length - 1
            If Split(strs(i), "drwxr-xr-x").Length = 2 Then
                dirslength += 1
            End If
        Next
        Dim dirs(dirslength - 1) As FtpDirectoryInfo
        Dim i2 As Integer = 0
        For i = 0 To strs.Length - 1
            If Split(strs(i), "drwxr-xr-x").Length = 2 Then
                Dim Str As String = strs(i).Split(":")(1)
                dirs(i2) = New FtpDirectoryInfo(Ftp & Str.Substring(2, Str.Length - 2).Substring(1, Str.Length - 3) & "/", Username, Password)
                i2 += 1
            End If
        Next
        Return dirs
    End Function
    Public Overrides Function ToString() As String
        Return FullName_
    End Function
End Class
Avatar billede MadsHaupt Juniormester
21. juli 2013 - 15:47 #1
svar
Avatar billede MadsHaupt Juniormester
10. august 2013 - 11:23 #2
Jeg har fundet ud af det, jeg var bare kommet til at bruge private shared i stedet for private.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester





White paper
Tidsbegrænset kampagne: Overvejer du at udskifte eller tilføje printere i din forretning? Vi kan tilbyde én eller flere maskiner gratis