C#怎样用代码将资源文件保存到本地?

RT
2024年11月28日 09:37
有1个网友回答
网友(1):

///


/// 下载远程图片
///
/// 将神桥要下载的图片地址
/// 返回string类型;图片的本地地址
public static string GetRemoteImage(string strUrl)
{
try
{
string fileExt = System.IO.Path.GetExtension(strUrl);
Random ro=new Random((int)DateTime.Now.Ticks);
Random ro2 = new Random(Guid.NewGuid().GetHashCode());
string PathStr =HttpContext.Current.Server.MapPath("/");
string filePath = @"\download\"+ DateTime.Now.ToString("yyyy") + @"\" + DateTime.Now.ToString("MMdd") + @"\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ro.Next(1000) + fileExt; string tempFolder = System.IO.Path.GetDirectoryName(PathStr + filePath);
if( !System.IO.Directory.Exists( tempFolder ) )
{
System.IO.Directory.CreateDirectory(tempFolder);
} WebClient wc = new WebClient();
wc.DownloadFile(strUrl,PathStr + filePath);
wc.Dispose();
return filePath;
}
catch
{
//System.Web.HttpContext.Current.Response.Write("\n");
//HttpContext.Current.Response.Write(Functions.ShowErr("获取远程图片出错!原因如下:\\n" + ,1));
//HttpContext.Current.Response.End();
return "";
}
}