0

我正在制作一个文件下载器,我希望它将文件下载到我选择的目录或默认目录。

下载代码在这里:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Button1.Enabled = False
    Button1.Text = "Updating..."
    WebBrowser1.Visible = True

    Dim uri As System.Uri = New System.Uri("http://199.91.154.170/e9f6poiwfocg/pei02c8727sa720/Ultra+v08.zip")
    Dim webclient As System.Net.WebClient = New System.Net.WebClient()

    Dim path As String = 
            If apppath = Nothing then
                New String(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "test\\Test.zip"))
            Else New String
                (apppath)
            End if

    Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(path)
    If Not System.IO.Directory.Exists(fileInfo.Directory.FullName) Then
        System.IO.Directory.CreateDirectory(fileInfo.Directory.FullName)
    End If

    AddHandler webclient.DownloadFileCompleted, AddressOf webclient_DownloadDataCompleted

    webclient.DownloadFileAsync(uri, path)

End Sub
Private Sub

用户选择的路径 apppath 在这里定义:

If apppath = "" Then
        Dim dialog As New FolderBrowserDialog()
        dialog.RootFolder = Environment.SpecialFolder.Desktop
        dialog.SelectedPath = Path.Combine(Environment.GetFolderPath( _
        Environment.SpecialFolder.ApplicationData))
        dialog.Description = "Select directory where to install the files"
        If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            apppath = dialog.SelectedPath
        End If
        My.Computer.FileSystem.WriteAllText(apppath & " apppath.txt", apppath, False)
    End If

如何修复 Dim path As String else 语句?

提前致谢!

4

1 回答 1

1

如果我正确理解了您的问题,那么我相信有一个简单的解决方案,那就是:

Dim path As String
If apppath = Nothing then
    path = New String(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "test\Test.zip"))
Else New String
    path = apppath
End if    

如果我没有正确理解您的要求,请提供更多信息。

于 2013-03-11T18:06:03.780 回答