Microsoft Graph SDK C#:将 FileSavePicker 与 Onedrive 结合使用

Microsoft Graph SDK C#: Use FileSavePicker with Onedrive

是否可以使用 FileSavePicker 对象通过适用于 C# 的 Microsoft Graph SDK 在 Onedrive 中保存文件。

原因是我需要用户select将存储数据备份文件的位置。

使用以下源代码,我可以下载或上传文件,但不会提示用户 select 所需的路径。

Authentication.cs

internal class Authentication
{

    internal static GraphServiceClient GetAuthenticatedClient()
    {
        if (Settings.graphClient == null)
        {
            try
            {
                Settings.graphClient = new GraphServiceClient("https://graph.microsoft.com/v1.0", new DelegateAuthenticationProvider(
                            async (requestMessage) =>
                            {
                                var token = await GetTokenForUserAsync();

                                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
                            })
                        );

                return Settings.graphClient;
            }
            catch (Exception)
            { }
        }
        return Settings.graphClient;
    }


    internal static async Task<string> GetTokenForUserAsync()
    {
        AuthenticationResult authResult;

        try
        {
            authResult = await Settings.IdentityClientApp.AcquireTokenSilentAsync(Settings.Scopes);

            Settings.TokenForUser = authResult.Token;
        }
        catch (Exception)
        {
            if (Settings.TokenForUser == null || Settings.Expiration <= DateTimeOffset.UtcNow.AddMinutes(5))
            {
                authResult = await Settings.IdentityClientApp.AcquireTokenAsync(Settings.Scopes);

                Settings.TokenForUser = authResult.Token;

                Settings.Expiration = authResult.ExpiresOn;
            }
        }

        return Settings.TokenForUser;
    }


    internal static void SignOut()
    {
        foreach (var user in Settings.IdentityClientApp.Users)

            user.SignOut();

        Settings.graphClient = null;

        Settings.TokenForUser = null;
    }

}

Settings.cs

internal class Settings
{
    public static string clientId = "xxxxxx-xxxx-xxx-xxx-xxxxxxxxx";

    public static string[] Scopes = {
                                        "https://graph.microsoft.com/User.Read",
                                        "https://graph.microsoft.com/Mail.Send",
                                        "https://graph.microsoft.com/Files.ReadWrite.All",
                                        "https://graph.microsoft.com/Files.ReadWrite" };

    public static PublicClientApplication IdentityClientApp = new PublicClientApplication(clientId);

    public static string TokenForUser = null;

    public static DateTimeOffset Expiration;

    public static GraphServiceClient graphClient = null;
}

下载文件

        public static async Task<bool> Download(string file, string to)
        {
            try
            {
                var download = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(file), Uri.EscapeUriString(System.IO.Path.GetFileName(file)));

                using (Stream stream = await Authentication.GetAuthenticatedClient().Me.Drive.Root.ItemWithPath(download).Content.Request().GetAsync())
                {
                    try
                    {
                        using (FileStream writer = new FileStream(to, FileMode.Create))
                        {

                        }

                        using (StreamReader reader = new StreamReader(stream))
                        {
                            await PathIO.WriteTextAsync(to, reader.ReadToEnd());
                        }

                        return true;
                    }
                    catch (Exception)
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

你的问题我不是很清楚;我了解到您想将 FilePicker 与 OneDrive 应用程序一起使用,然后强制用户 select 成为 OneDrive 中的目标?

这是不可能的。我认为最好的方法是构建您自己的 UI 让用户 select 一个文件夹并使用上面的代码将备份文件保存到 onedrive