gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:11:45 24 4
gpt4 key购买 nike

我编写了一个连接到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com