在VBA中使用C#DLL

问题描述

我有一个用C#编写的类库,该类库使用Google Drive v3 API来保持本地文件存储库的更新。这段代码在控制台应用程序中运行良好,但是当我在应用必要的措施(将其注册等)后在VBA工具中向该DLL添加引用时,它不会在VBA中运行updateFiles方法。但是,它会运行不依赖于Google Drive API的其他功能。我的问题是,如何使该dll在VBA中工作。如果有人能指出我正确的方向,那就太好了。

预先感谢

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace fileUpdater
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class Class1
    {
        private static DriveService service;
        private static Dictionary<string,Google.Apis.Drive.v3.Data.File> files = new Dictionary<string,Google.Apis.Drive.v3.Data.File>();
        private const string FOLDER_MIME_TYPE = "application/vnd.google-apps.folder";
        private static double percentComplete;

        private string AbsPath(Google.Apis.Drive.v3.Data.File file)
        {
            var name = file.Name;

            if (file.Parents.Count() == 0)
            {
                return name;
            }

            var path = new List<string>();

            while (true)
            {
                var parent = GetParent(file.Parents[0]);

                if (parent.Parents == null || parent.Parents.Count() == 0)
                {
                    break;
                }

                path.Insert(0,parent.Name);
                file = parent;
            }
            path.Add(name);
            return path.Aggregate((current,next) => Path.Combine(current,next));
        }

        private Google.Apis.Drive.v3.Data.File GetParent(string id)
        {
            if (files.ContainsKey(id))
            {
                return files[id];
            }

            var request = service.Files.Get(id);
            request.Fields = "name,parents";
            var parent = request.Execute();

            files[id] = parent;

            return parent;
        }

        public void updateFiles()
        {
            service = AuthenticateServiceAccount("serviceAccountEmail",@"serviceAccountCredentials");
            if (service == null)
            {
                return;
            }
            FilesResource.ListRequest listRequest = service.Files.List();
            listRequest.Fields = "files(id,name,mimeType,parents,createdTime)";
            listRequest.PageSize = 1000;
            IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
                .Files;
            double counter = 0.0f;
            foreach (var file in files)
            {
                percentComplete = Math.Round((counter / files.Count) * 100,2);
                if (file.Parents != null)
                {
                    string path = "downloadFile/" + AbsPath(file);
                    if (file.MimeType == FOLDER_MIME_TYPE)
                    {
                        if (!System.IO.Directory.Exists(path))
                        {
                            System.IO.Directory.CreateDirectory(path);
                        }
                    }
                    else
                    {
                        if (System.IO.File.Exists(path))
                        {
                            if (DateTime.Compare((DateTime)file.CreatedTime,System.IO.File.GetCreationTime(path)) > 0)
                            {
                                System.IO.File.Delete(path);
                                downloadFile(file,path);
                            }
                        }
                        else
                        {
                            downloadFile(file,path);
                        }
                    }
                }
                counter++;
            }
        }

        private void downloadFile(Google.Apis.Drive.v3.Data.File file,string path)
        {
            var stream = new System.IO.MemoryStream();

            service.Files.Get(file.Id).Download(stream);
            System.IO.FileStream fileToWrite = new System.IO.FileStream(path,System.IO.FileMode.Create,System.IO.FileAccess.Write);

            stream.WriteTo(fileToWrite);

            fileToWrite.Close();
        }


        private DriveService AuthenticateServiceAccount(string serviceAccountEmail,string keyFilePath)
        {
            if (!System.IO.File.Exists(keyFilePath))
            {
                return null;
            }

            string[] scopes = new string[] { DriveService.Scope.Drive };

            var certificate = new X509Certificate2(keyFilePath,"password",X509KeyStorageFlags.Exportable);
            try
            {
                ServiceAccountCredential credential = new ServiceAccountCredential(
                    new ServiceAccountCredential.Initializer(serviceAccountEmail)
                    {
                        Scopes = scopes
                    }.FromCertificate(certificate));
                DriveService service = new DriveService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,ApplicationName = "Drive API Sample"
                });
                return service;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                return null;
            }
        }

        public double getPercentComplete()
        {
            return percentComplete;
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)