簡體   English   中英

使用流時的 C# System.UnauthorizedAccessException

[英]C# System.UnauthorizedAccessException when using stream

我正在嘗試通過我的 C# 程序將一些文件上傳到 Google Drive。 我正在使用流與 Google Drive API 功能一起上傳文件。 但我有一個名為 System.UnauthorizedAccessException 的異常。 但是當我使用 File.ReadAllText 函數時,我沒有得到異常。 這是我的代碼,異常在第 7 行。謝謝您的回答。

public void uploadFile(string path, DriveService service)
{
    var fileMetadata = new Google.Apis.Drive.v3.Data.File();
    fileMetadata.Name = Path.GetFileName(path);
    fileMetadata.MimeType = "txt";
    FilesResource.CreateMediaUpload request;
    using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Open))
    {
        request = service.Files.Create(fileMetadata, stream, "txt");
        request.Fields = "id";
        request.Upload();
    }

    var file = request.ResponseBody;
}

編輯 1:

我的項目有指向 GitHub 的完整源代碼的鏈接: https : //github.com/Nextesro/IDK-client

使用File.OpenRead打開文件以進行只讀訪問。

using (var stream = File.OpenRead(path))
{
    // ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM