在VB中将控制台应用程序代码转换为Windows窗体代码

时间:2014-04-30 12:43:47

标签: vb.net winforms console google-drive-api

每一个人。我谦卑地想要求你的支持。 我正在努力学习Google云端硬盘SDK并遵循视频和页面中提供的所有步骤: https://developers.google.com/drive/web/quickstart/quickstart-cs

然而,我已经停止了,因为我意识到提供的示例代码是CSharp而没有VB版本。 我已经尝试使用Telerik转换器将其转换为VB,但结果并不令人鼓舞,因为我仍然在那里看到大括号,什么不是。我知道我看到了这些,因为它是一个控制台应用程序,我会很诚恳,我不太擅长控制台应用程序代码。

因此,我谦卑地寻求有关如何将已经转换的C#转换为VB代码的帮助 到Windows窗体版本。谢谢。

Namespace GoogleDriveSamples
    Class DriveCommandLineSample
        Private Shared Sub Main(args As String())
            Dim credential As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With { _
                Key .ClientId = "CLIENT_ID_HERE", _
                Key .ClientSecret = "CLIENT_SECRET_HERE" _
            }, New () {DriveService.Scope.Drive}, "user", CancellationToken.None).Result

            ' Create the service.
            Dim service = New DriveService(New BaseClientService.Initializer() With { _
                Key .HttpClientInitializer = credential, _
                Key .ApplicationName = "Drive API Sample" _
            })

            Dim body As New File()
            body.Title = "My document"
            body.Description = "A test document"
            body.MimeType = "text/plain"

            Dim byteArray As Byte() = System.IO.File.ReadAllBytes("document.txt")
            Dim stream As New System.IO.MemoryStream(byteArray)

            Dim request As FilesResource.InsertMediaUpload = service.Files.Insert(body, stream, "text/plain")
            request.Upload()

            Dim file As File = request.ResponseBody
            Console.WriteLine("File id: " + file.Id)
            Console.WriteLine("Press Enter to end this process.")
            Console.ReadLine()
        End Sub
    End Class
End Namespace

2 个答案:

答案 0 :(得分:0)

云端硬盘SDK(或任何其他谷歌SDK)目前不支持vb。使用c#/ .net这样做,或者如果必须,可以直接从任何语言执行https调用(直接获取帖子等),甚至vb。

答案 1 :(得分:0)

Option Infer On

Imports System.Threading
Imports System.Threading.Tasks
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Drive.v2
Imports Google.Apis.Drive.v2.Data
Imports Google.Apis.Services

Public Class Form1

Private Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
    Dim secrets = New ClientSecrets()
    secrets.ClientId = "CLIENT_ID_HERE"
    secrets.ClientSecret = "CLIENT_SECRET_HERE"

    Dim scope = New List(Of String)
    scope.Add(DriveService.Scope.Drive)

    Dim credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, scope, "user", CancellationToken.None).Result()

    Dim initializer = New BaseClientService.Initializer
    initializer.HttpClientInitializer = credential
    initializer.ApplicationName = "Drive API Sample"

    Dim service = New DriveService(initializer)

    Dim body = New File
    body.Title = "My document"
    body.Description = "A test document"
    body.MimeType = "text/plain"

    Dim byteArray = System.IO.File.ReadAllBytes("C:\MyFile.txt")
    Dim stream = New System.IO.MemoryStream(byteArray)

    Dim request = service.Files.Insert(body, stream, "text/plain")
    request.Upload()

    body = request.ResponseBody
    MessageBox.Show("The File id is:" & body.Id, "Upload Succeeded")
End Sub

End Class