问题描述
我想创建Windows服务,该服务从URL下载文件并将其存储到本地路径.
我的代码就像...
< pre>
字符串destPath = Environment.SystemDirectory + @"\ oobe \ Info \ backgrounds \";
如果(!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
流str = null;
字符串_URL ="http://localhost/exercise2/bin-debug/Background/backgroundDefault.jpg";
//创建一个包含图片的网址的Web请求
HttpWebRequest wReq =(HttpWebRequest)WebRequest.Create(_URL);
//从网络请求中获取响应
HttpWebResponse wRes =(HttpWebResponse)(wReq).GetResponse();
//从先前指定的URL返回图像流
str = wRes.GetResponseStream();
如果(str!= null)
{
Image.FromStream(str).Save(destPath +"backgroundDefault.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
str.Flush();
}
</pre>
当服务执行该时间时,会出现类似
的错误错误! GDI +中发生一般错误.
关于在一定间隔时间内为下载文件创建Windows服务并将其存储在本地的任何想法????
帮帮我....
I want to create windows service which download file from URL and store it into local path.
My code is like...
<pre>
string destPath = Environment.SystemDirectory + @"\oobe\Info\backgrounds\";
if (!Directory.Exists(destPath))
{
Directory.CreateDirectory(destPath);
}
Stream str = null;
string _URL = "http://localhost/exercise2/bin-debug/Background/backgroundDefault.jpg";
//Create a web request to the url containing the image
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(_URL);
//gets the response from the web request
HttpWebResponse wRes = (HttpWebResponse)(wReq).GetResponse();
//return the image stream from the URL specified earlier
str = wRes.GetResponseStream();
if (str != null)
{
Image.FromStream(str).Save(destPath + "backgroundDefault.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
str.Flush();
}
</pre>
When service execute that time it gives error like
Error! A generic error occurred in GDI+.
Any Idea about creating windows service for download file in some interval time and store it in local location????
Help me....
推荐答案
这篇关于Windows服务,带有从URL下载文件并将其存储在本地位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!