using System.Net;
namespace DcmToPng.Helper
{
public static class DownloadHelper
{
///
/// 下载
///
/// Dictionary(文件名称.dcm,共享文件夹路径)
public static void Down(Dictionary dcmPathList)
{
if (!Directory.Exists(Constant.DcmPath))
Directory.CreateDirectory(Constant.DcmPath);
var i = 0;
foreach (var item in dcmPathList)
{
try
{
if (File.Exists(Path.Combine(Constant.DcmPath, item.Key))) continue;
using var client = new WebClient();
// 配置授权账户密码
var credentials = new NetworkCredential(Constant.UserName, Constant.Password);
client.Credentials = credentials;
var buffer = client.DownloadData(item.Value);
var isSave = Bytes2File(buffer, Constant.DcmPath, item.Key);
if (isSave) i++;
}
catch (Exception e)
{
Console.WriteLine($"[Down][Error]" + item.Key + " | " + e.Message);
}
}
Console.WriteLine($"[DCM文件下载成功]" + i);
}
///
/// 将byte数组转换为文件并保存到指定地址
///
/// byte数组
/// 保存地址
///
public static bool Bytes2File(byte[] buff, string path, string fileName)
{
try
{
//如果不存在就创建Enclosure文件夹
if (Directory.Exists(path) == false)
Directory.CreateDirectory(path);
if (File.Exists(path + fileName))
File.Delete(path + fileName);
var fs = new FileStream(path + fileName, FileMode.CreateNew);
var bw = new BinaryWriter(fs);
bw.Write(buff, 0, buff.Length);
bw.Close();
fs.Close();
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
}
}