Google Drive API in VB.net check if file is opened

Actarus 1 Reputation point
2022-03-25T06:34:07.28+00:00

Hi to all, I need to write inside a file located in a folder of my account in google drive and I wrote a desktop application in VB.net using Google Driver API v3. Since that me and my friend use the same account in different PC with the same VB.net application that I wrote, I need your help to to check if that file is opened by another user to avoid opening and garantee an "exclusive opening" on it.

I dont' know if google drive as an option to set an "exclusive opening" instead of API.

Maybe I can set permission to "read only" on the file when is opened by my application but I think it is not a good thing because if my application crashes (for example for a blackout) my file remain in read only state for always.

That's my routine that make a connection on my Google drive with my credentials, search for my folder and my file and give me back their ID:

Public Sub GoogleDrive()
Dim credential As UserCredential
Dim ID_Folder As String = ""
Dim ID_File As String = ""

Using Stream = New FileStream("credenzials.json", FileMode.Open, FileAccess.Read)
'The file token.json stores the user's access and refresh tokens, and is created
'automatically when the authorization flow completes for the first time.
Dim credPath As String = "token.json"
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(Stream).Secrets,
Scopes,
"user",
CancellationToken.None,
New FileDataStore(credPath, True)).Result
Console.WriteLine("Credential file saved to: " + credPath)

End Using
'Create Drive API service.
Dim Service = New DriveService(New BaseClientService.Initializer() With
{
.HttpClientInitializer = credential,
.ApplicationName = ApplicationName
})
' Define parameters of request.
Try
Dim findrequest As FilesResource.ListRequest = Service.Files.List()
findrequest.PageSize = 10
findrequest.Fields = "nextPageToken, files(id, name)"
findrequest.Spaces = "drive"
Dim listFolder As Data.FileList = findrequest.Execute()

If listFolder.Files.Count > 0 Then
For Each item In listFolder.Files
If item.MimeType = "application/vnd.google-apps.folder" Then
If item.Name = "MyFolder" Then
ID_Folder = item.Id.ToString
End If
End If
'
If item.MimeType = "application/msaccess" Then
If item.Name = "myFile.mdb" Then
ID_File = item.Id.ToString
End If
End If

If (ID_File <> "") And (ID_Folder <> "") Then
'How check if ID_File is opened by an other user ?
Exit For
End If
Next
End If

Catch ex As Exception
'MsgBox(ex.Message)
Throw ex

End Try
End Sub

Can someone help me ?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,523 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,668 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Actarus 1 Reputation point
    2022-03-25T12:19:37.557+00:00

    Thank you for the tip

    0 comments No comments