c# - 无法以编程方式下载文件 (C#)

标签 c# azure azure-storage azure-blob-storage

我编写了一个连接到 Azure 存储帐户的 C# 程序。

给定一个 blob URI,我需要将该文件下载到本地文件并执行它。

这是我的代码:

var blobClientCode = client.CreateCloudBlobClient();
CloudBlockBlob codeBlob = blobClientCode.GetBlockBlobReference(codeUri);
File.Create("C:\\code.exe");
using (var fileStream = File.OpenWrite("C:\\code.exe")) {
     codeBlob.DownloadToStream(fileStream);
}

Process p = new Process();
p.StartInfo.FileName = "C:\\mycode.exe";
p.StartInfo.Arguments = dataUri;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();

问题是我不断收到 UnauthorizedAccess 异常。

  • 当我尝试从浏览器手动下载文件(复制并粘贴 URI)时,我成功了。
  • 该容器是公共(public)容器。
  • 我还尝试使用 WebClient.DownloadFile(),但得到了 WebException。

我错过了什么? 提前致谢

最佳答案

在调用 webclient.DownloadFile 之前尝试包含下面提到的代码片段。希望它能起作用..

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(delegate { return true; });

 WebClient webclient = new WebClient();
webclient.DownloadFile(new Uri(URIPath), LocalPath);

注意:如果您使用代理访问互联网,您可能需要设置代理设置

WebProxy ProxyObject = ProxySetting;
webclient.Proxy = ProxySetting

这基本上会导航页面,无论证书是否经过验证。

关于c# - 无法以编程方式下载文件 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15996721/

相关文章:

c# - 如何使用 Windows 7 连接到 C# 中的 websocket?

azure - 我的订阅已被删除,但仍显示在 Azure 门户中

asp.net-mvc-3 - 调整我部署到 Azure 的 MVC3 应用程序

c# - 当您只想更改返回类型时重载方法的最佳方法

c# - 命名数组中的每个元素

c# - 如何将 Press Enter Key 发送到 Web 浏览器控件中的 htmlelement

r - 如何使用 arrow::open_dataset 连接到 Azure Blob 存储中的 Parquet 文件?

azure - 将在 Azure Web 作业中执行的 C# Selenium 自动化应用程序

针对 VM 的 Azure 负载均衡器入站 NAT 规则

azure - Microsoft Azure RA-GRS 存储 - 在 Microsoft 管理的故障转移到次要区域后,数据是否仍进行异地复制?