Need help with C# Google Drive integration
I’m working on a C# application that uploads CSV files to Google Drive. The problem is that every time I run my program, it creates a new folder with the same name instead of using the existing one. I have written some code to check if the folder exists before creating it, but something is not working right.
class DriveManager
{
private string[] ApiScopes = { DriveService.Scope.Drive };
private string AppName;
private string DirectoryId;
private string DocumentName;
private string DocumentPath;
private string FileType;
public DriveManager(string docName, string docPath)
{
AppName = "DataUploader";
FileType = "text/csv";
DocumentName = docName;
DocumentPath = docPath;
}
public void ExecuteUpload()
{
UserCredential auth = AuthenticateUser();
DriveService driveClient = InitializeDriveService(auth);
DirectoryId = SetupDirectory(driveClient, "DataStorage");
TransferFile(driveClient, DocumentName, DocumentPath, FileType);
}
private DriveService InitializeDriveService(UserCredential auth)
{
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = auth,
ApplicationName = AppName,
});
}
private UserCredential AuthenticateUser()
{
UserCredential auth;
using (var fileStream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
string tokenPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
tokenPath = Path.Combine(tokenPath, ".tokens/drive-uploader.json");
auth = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(fileStream).Secrets,
ApiScopes,
"user",
CancellationToken.None,
new FileDataStore(tokenPath, true)).Result;
}
return auth;
}
private string TransferFile(DriveService driveClient, string docName, string docPath, string fileType)
{
var fileProperties = new Google.Apis.Drive.v3.Data.File();
fileProperties.Name = docName;
fileProperties.Parents = new List<string> { DirectoryId };
FilesResource.CreateMediaUpload uploadRequest;
using (var fileStream = new FileStream(docPath, FileMode.Open))
{
uploadRequest = driveClient.Files.Create(fileProperties, fileStream, fileType);
uploadRequest.Upload();
}
var uploadedFile = uploadRequest.ResponseBody;
return uploadedFile.Id;
}
public static string SetupDirectory(DriveService driveClient, string directoryName)
{
bool folderExists = CheckDirectoryExists(driveClient, directoryName);
if (folderExists)
return $"Directory {directoryName} is already present!";
var newDirectory = new Google.Apis.Drive.v3.Data.File();
newDirectory.Name = directoryName;
newDirectory.MimeType = "application/vnd.google-apps.folder";
var createRequest = driveClient.Files.Create(newDirectory);
createRequest.Fields = "id";
var response = createRequest.Execute();
return response.Id;
}
private static bool CheckDirectoryExists(DriveService driveClient, string directoryName)
{
var searchRequest = driveClient.Files.List();
searchRequest.PageSize = 100;
searchRequest.Q = $"trashed = false and name contains '{directoryName}' and 'root' in parents";
searchRequest.Fields = "files(name)";
var foundFiles = searchRequest.Execute().Files;
foreach (var foundFile in foundFiles)
{
if (directoryName == foundFile.Name)
return true;
}
return false;
}
}
Can someone help me figure out why the duplicate check is not working properly?