下载文本文件并设置组件?

时间:2014-03-04 12:56:20

标签: vb.net

如何让vb下载文本文件,然后阅读所有行以查看可用的组件,然后在form上设置它们?文本文件将包含以下内容:

  • [1] =组件数,pyc_file作为要显示给用户的组件名称。最后是从下载文件的链接。

pyc_file旁边应显示checkbox

[1];pyc_file;www.mediafire.com/abcd1234/_init_.pyc;

我不知道如何解释它。我希望你明白!

2 个答案:

答案 0 :(得分:0)

您可以创建自己的类型来解析这些部分:

enter image description here

Public Class Form1

    Dim LineToParse As String =
        "[1];pyc_file;www.mediafire.com/abcd1234/_init_.pyc;"

    Private Sub Test() Handles MyBase.Shown

        Dim Comp As Component = Me.GetComponent(LineToParse)

        Dim sb As New System.Text.StringBuilder

        sb.AppendFormat("Line: {0}", LineToParse)
        sb.AppendLine(Environment.NewLine)

        sb.AppendFormat("Index: {0}", CStr(Comp.Index))
        sb.AppendLine()
        sb.AppendFormat("Name: {0}", Comp.Name)
        sb.AppendLine()
        sb.AppendFormat("Url: {0}", Comp.URL)

        MessageBox.Show(sb.ToString, "Component information",
                        MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    ''' <summary>
    ''' Parses a comma-delimited component text-line and returns a Component Object.
    ''' </summary>
    ''' <param name="ComponentLine">Indicates the component line to parse.</param>
    ''' <returns>Component.</returns>
    Friend Function GetComponent(ByVal ComponentLine As String) As Component

        Dim ComponentParts As IEnumerable(Of String) =
            ComponentLine.Split(";"c)

        Dim Index As Integer =
            Integer.Parse(ComponentParts(0).
                          Replace("[", String.Empty).Replace("]", String.Empty))

        Dim Name As String =
            ComponentParts(1)

        Dim URL As Uri =
            New Uri(If(Not ComponentParts(2).StartsWith("http://", StringComparison.OrdinalIgnoreCase),
                       String.Format("http://{0}", ComponentParts(2)),
                       ComponentParts(2)))

        Return New Component With
               {
                 .Index = Index,
                 .Name = Name,
                 .URL = URL
               }

    End Function

    ''' <summary>
    ''' A component.
    ''' </summary>
    Friend NotInheritable Class Component

        ''' <summary>
        ''' Indicates the number of component.
        ''' </summary>
        Public Property Index As Integer

        ''' <summary>
        ''' Indicates the component name to display to user.
        ''' </summary>
        Public Property Name As String

        ''' <summary>
        ''' Indicates a link from where the file is downloaded.
        ''' </summary>
        Public Property URL As Uri

    End Class

End Class

答案 1 :(得分:0)

我现在已经解决了:-) 实际上并没那么难。