如何声明公共变量,如果函数避免在vb

时间:2016-11-14 01:09:13

标签: vb.net dropbox dropbox-api

嘿,我在编程的中间,但突然我注意到我的变量没有检测到。所以我的语法参考变得不同,就像我不能在其他函数中使用A.User.Async。 这是我的代码,我想将变量" A作为新的dropboxclient(storedaccestoken)"作为公共变量,但是当我启动它时,storedaccestoken为null,因此流程已损坏。这个代码是我的应用程序运行的版本,当我尝试创建变量" A"成为公共的"存储的,并且#34;总是回来null,我的应用程序不会开始。请帮我找到解决方案

Imports System.IO
Imports System.Text
Imports System.Net
Imports login.Login
Imports Dropbox
Imports Dropbox.Api
Public Class menuutama

    Dim IList As Object
    Dim sourcepath As String
    Dim FILE_NAME As String
    Dim storedaccesstoken As String
    Dim root
    Dim folder



    Sub New()
        InitializeComponent()
        sourcepath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        FILE_NAME = sourcepath & "\myaccestoken.txt"
        root = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    folder = root & "\Dropbox"



End Sub


    Private Sub menuutama_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        storedaccesstoken = My.Computer.FileSystem.ReadAllText(FILE_NAME)

        If Not FileLen(FILE_NAME) = 0 Then

        Dim A As New DropboxClient(storedaccesstoken)
        Dim info = A.Users.GetCurrentAccountAsync
            akun.Text = "Name : " & info.Result.Name.DisplayName & vbCrLf
            akun.Text += "Email : " & info.Result.Email & vbCrLf
            Dim Space = A.Users.GetSpaceUsageAsync

            ruangkosong.Text = "Used Spaces : " & filesize(Space.Result.Used) & " From " & filesize(Space.Result.Allocation.AsIndividual.Value.Allocated)

            For Each File In A.Files.ListFolderAsync(String.Empty).Result.Entries()
                Dim NewItem As New ListViewItem
                NewItem.Text = Path.GetFileName(File.Name)
                If File.IsFolder Then
                    NewItem.SubItems.Add("Directory")
                    NewItem.ImageIndex = 1
                Else
                    NewItem.SubItems.Add("File")
                    NewItem.SubItems.Add(filesize(File.AsFile.Size))
                    NewItem.ImageIndex = 2
                End If
                listfile.Items.Add(NewItem)
            Next

        End If
    End Sub
    'membuat fungsi filesize dimana mengambil ukuran dari file yang nanti kita pilih
    Private Function filesize(ByVal size As Double) As String
        Dim type As String() = {"B", "KB", "MB", "GB"}
        Dim sizedouble As Double = size
        Dim CSA As Integer = 0
        While sizedouble >= 1024 AndAlso CSA + 1 < type.Length
            CSA += 1
            sizedouble = sizedouble / 1024
        End While
        Return [String].Format("{0:0.##} {1}", sizedouble, type(CSA))
    End Function

    Private Sub UploadToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles UploadToolStripMenuItem1.Click
        Using FolderBrowserDialog As New FolderBrowserDialog
            If FolderBrowserDialog.ShowDialog() <> Windows.Forms.DialogResult.OK Then Return
            Dim alamat As String = FolderBrowserDialog.SelectedPath
            Dim nama As String = New DirectoryInfo(alamat).Name
            Dim FileCount As Integer = 0
            My.Computer.FileSystem.CopyDirectory(alamat, folder & "/" + nama, True)
            MsgBox("Successfully Uploaded")
        End Using
        listfile.Items.Clear()
        For Each File In A.Files.ListFolderAsync(String.Empty).Result.Entries()
            Dim NewItem As New ListViewItem
            NewItem.Text = Path.GetFileName(File.Name)
            If File.IsFolder Then
                NewItem.SubItems.Add("Directory")
                NewItem.ImageIndex = 1
            Else
                NewItem.SubItems.Add("File")
                NewItem.SubItems.Add(filesize(File.AsFile.Size))
                NewItem.ImageIndex = 2
            End If
            listfile.Items.Add(NewItem)
        Next
    End Sub

    Private Sub listfile_DoubleClick(sender As Object, e As EventArgs) Handles listfile.DoubleClick

        For Each C As ListViewItem In listfile.SelectedItems
            On Error Resume Next
            If C.Text = "..." Then

                listfile.Items.Clear()

                If Not direktori.Text.Substring(0, direktori.Text.LastIndexOf("/")) = Nothing Then
                    Dim NewItem As New ListViewItem
                    NewItem.Text = "..."
                    NewItem.ImageIndex = 2
                    listfile.Items.Add(NewItem)
                End If

                direktori.Text = direktori.Text.Substring(0, direktori.Text.LastIndexOf("/"))

                For Each File In A.Files.ListFolderAsync(direktori.Text).Result.Entries
                    Dim NewItem As New ListViewItem
                    NewItem.Text = Path.GetFileName(File.Name)
                    If File.IsFolder Then
                        NewItem.SubItems.Add("Directory")
                        NewItem.ImageIndex = 0
                    Else
                        NewItem.SubItems.Add("File")
                        NewItem.SubItems.Add(filesize(File.AsFile.Size))
                        NewItem.ImageIndex = 1
                    End If
                    listfile.Items.Add(NewItem)
                Next

            Else

                listfile.Items.Clear()

                Dim back As New ListViewItem
                back.Text = "..."
                back.ImageIndex = 2
                listfile.Items.Add(back)

                direktori.Text += "/" & C.Text

                For Each File In A.Files.ListFolderAsync(direktori.Text).Result.Entries
                    Dim NewItem As New ListViewItem
                    NewItem.Text = Path.GetFileName(File.Name)
                    If File.IsFolder Then
                        NewItem.SubItems.Add("Directory")
                        NewItem.ImageIndex = 0
                    Else
                        NewItem.SubItems.Add("File")
                        NewItem.SubItems.Add(filesize(File.AsFile.Size))
                        NewItem.ImageIndex = 1
                    End If
                    listfile.Items.Add(NewItem)
                Next

            End If

        Next

    End Sub
    Private Sub LoginToolStripMenuItem_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub CountFiles(InFolder As String, ByRef Result As Integer)
        Result += IO.Directory.GetFiles(InFolder).Count
        For Each f As String In IO.Directory.GetDirectories(InFolder)
            CountFiles(f, Result)
        Next
    End Sub


    Private Sub LogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogoutToolStripMenuItem.Click

        Dim wHeader As WebHeaderCollection = New WebHeaderCollection()
        Dim wProxy As IWebProxy = WebRequest.GetSystemWebProxy()
        wProxy.Credentials = System.Net.CredentialCache.DefaultCredentials

        wHeader.Clear()
        wHeader.Add("Authorization: Bearer " + storedaccesstoken)
        Dim sUrl As String = "https://api.dropboxapi.com/2/auth/token/revoke"

        Dim wRequest As HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(sUrl), HttpWebRequest)

        wRequest.Headers = wHeader
        wRequest.Method = "POST"
        wRequest.Proxy = wProxy


        Dim wResponse As HttpWebResponse = DirectCast(wRequest.GetResponse(), HttpWebResponse)

        Dim sResponse As String = ""

        Using srRead As New StreamReader(wResponse.GetResponseStream())
            sResponse = srRead.ReadToEnd()
        End Using
        If System.IO.File.Exists(FILE_NAME) = True Then
            System.IO.File.Delete(FILE_NAME)
        End If
        Dim login As New Login
        login.Show()
        Me.Close()
    End Sub

    Private Sub CobaToolStripMenuItem_Click(sender As Object, e As EventArgs)

        Using FolderBrowserDialog As New FolderBrowserDialog
            If FolderBrowserDialog.ShowDialog() <> Windows.Forms.DialogResult.OK Then Return
            Dim alamat As String = FolderBrowserDialog.SelectedPath
            Dim nama As String = New DirectoryInfo(alamat).Name
            Dim FileCount As Integer = 0
            My.Computer.FileSystem.CopyDirectory(alamat, folder & "/" + nama, True)
        End Using
    End Sub



    Private Sub GetShareLinkToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GetShareLinkToolStripMenuItem.Click
        For Each C As ListViewItem In listfile.SelectedItems

            Dim GetLink = A.Sharing.CreateSharedLinkAsync(direktori.Text & "/" & C.Text)
            MsgBox(GetLink.Result.Url)
            Dim Path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            Dim FILE_NAME = Path & "\link.txt"

            If System.IO.File.Exists(FILE_NAME) = False Then
                System.IO.File.Create(FILE_NAME).Dispose()
            End If

            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, False)
            objWriter.Write(GetLink.Result.Url.ToString)
            objWriter.Close()


        Next
    End Sub

    Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click
        For Each C As ListViewItem In listfile.SelectedItems
            Dim Del = A.Files.DeleteAsync(direktori.Text & "/" & C.Text)
            C.Remove()
            MsgBox("Successully Deleted")
        Next
    End Sub

    Private Sub UploadToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles UploadToolStripMenuItem2.Click
        Dim C As New OpenFileDialog
        C.Title = "Choose File"
        C.Filter = "All Files (*.*)|*.*"
        If C.ShowDialog = Windows.Forms.DialogResult.OK Then
            UploadFile(C.FileName)
        End If
    End Sub


    Async Sub UploadFile(Filepath As String)
        Dim Up = Await A.Files.UploadAsync(direktori.Text & "/" & Path.GetFileName(Filepath), body:=(New FileStream(Filepath, FileMode.Open, FileAccess.Read)))

        Dim NewItem As New ListViewItem
        NewItem.Text = Path.GetFileName(Up.Name)
        NewItem.SubItems.Add("File")
        NewItem.SubItems.Add(filesize(Up.Size))
        NewItem.ImageIndex = 1
        listfile.Items.Add(NewItem)

        MsgBox("Successfully Uploaded")

    End Sub

    Private Sub DownloadToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles DownloadToolStripMenuItem2.Click
        For Each D As ListViewItem In listfile.SelectedItems
            Dim C As New SaveFileDialog
            C.Title = " Save File To"
            C.Filter = "All Files (*.*)|*.*"
            C.FileName = Path.GetFileName(D.Text)
            If C.ShowDialog = Windows.Forms.DialogResult.OK Then
                DownloadFile(D.Text, C.FileName)
            End If
        Next
    End Sub

    Async Sub DownloadFile(Filepath As String, Final As String)
        Dim Down = Await A.Files.DownloadAsync(direktori.Text & "/" & Path.GetFileName(Filepath))
        File.WriteAllBytes(Final, Await Down.GetContentAsByteArrayAsync)
        MsgBox("Successfully Downloaded")
    End Sub
End Class

0 个答案:

没有答案