/// <summary>
/// 保存互联网图片
/// </summary>
/// <param name="url">文件原始路径"如:http://www.baidu.com/1.png"</param>
/// <param name="file">文件保存路径“如:D:\\ceshi\\1.png”</param>
/// <returns></returns>
public static bool DownFile(string url, string file)
{
WebClient wc = new WebClient();
bool reslut = true;
try
{
wc.DownloadFile(url, file);
}
catch (Exception e)
{
reslut = false;
}
return reslut;
}

winfrom

private void getfile_Click(object sender, EventArgs e)
{

string fileURL=this.fileURL.Text.Trim();
string[] type = fileURL.Split('.');
int i = 1;
string filePath = "D:\\ceshi\\1."+ type[type.Length - 1];
DownFile(fileURL, filePath);
}

获取该文件的类型

打开文件流后,一定要关闭他

/// <summary>
/// 保存互联网图片
/// </summary>
/// <param name="url">文件原始路径"如:http://www.baidu.com/1.png"</param>
/// <param name="file">文件保存路径“如:D:\\ceshi\\1.png”</param>
/// <returns></returns>
public static bool DownFile(string url, string file)
{
WebClient wc = new WebClient();
bool reslut = true;
string fileType = "";
try
{
Stream wcOpen= wc.OpenRead(url); //打开流
fileType = wc.ResponseHeaders["Content-Type"].Split('/')[1]; //获取类型
file = file + "1." + fileType;
wcOpen.Close(); //关闭流
wc.DownloadFile(url, file);

}
catch (Exception e)
{
reslut = false;
}
return reslut;
}