C#下载一个网页上的小文件
由于种种原因不想再用易语言开发软件了,现在试着使用c#编写一款音乐下载软件下载文件用易语言是这样写的:写到文件 (目录, 到字节集 (网页_访问_对象 (下载地址)))
想着用c#应该是System.IO中的File.Create或File.Copy方法
结果都没有做出来
另外易语言中的网页_访问_对象不知道用c#怎么写
求助大神
public partial class Form1 : Form
{
string str;
public string Str
{
get { return str; }
set { str = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] index;
index=System.IO.Directory.GetFiles(@"网站的路径","*.*",System.IO.SearchOption.AllDirectories);
for (int i = 0; i < index.Length; i++) {
if (index.Length > 2300)
{
Str += index;
//Str += "<br>";
}
}
label1.Text = Str;
}
} 或者用第三方库,https://github.com/UweKeim/ZetaLongPaths 本帖最后由 coolcalf 于 2020-1-21 16:40 编辑
/// <summary>
/// c#,.net 下载文件
/// </summary>
/// <param name="URL">下载文件地址</param>
///
/// <param name="Filename">下载后的存放地址</param>
/// <param name="Prog">用于显示的进度条</param>
///
public static void DownloadFile(string URL, string filename, System.Windows.Controls.ProgressBar prog, System.Windows.Controls.Label label1)
{
float percent = 0;
try
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
long totalBytes = myrp.ContentLength;
if (prog != null)
{
prog.Maximum = (int)totalBytes;
}
System.IO.Stream st = myrp.GetResponseStream();
System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
long totalDownloadedByte = 0;
byte[] by = new byte;
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
//System.Windows.Forms.Application.DoEvents();
so.Write(by, 0, osize);
if (prog != null)
{
prog.Value = (int)totalDownloadedByte;
}
osize = st.Read(by, 0, (int)by.Length);
percent = (float)totalDownloadedByte / (float)totalBytes * 100;
label1.Content = "当前补丁下载进度" + percent.ToString() + "%";
//System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
}
so.Close();
st.Close();
}
catch (System.Exception)
{
throw;
}
}
需要引用:
PresentationCore
PresentationFramework
WindowsBase
因为是一个实例中的代码,略有点乱,供参考
#region 简单GET方式请求 +GetUrlHtml(string url, Encoding readCoding, int timeout = 30000,string userAgent = "", CookieCollection cookies = null)
/// <summary>
/// 简单GET方式请求 +GetUrlHtml(string url, Encoding readCoding, int timeout = 30000,string userAgent = "", CookieCollection cookies = null)
/// </summary>
/// <param name="url"></param>
/// <param name="readCoding"></param>
/// <param name="timeout"></param>
/// <param name="userAgent"></param>
/// <param name="cookies"></param>
/// <returns></returns>
public static string GetUrlHtml(string url, Encoding readCoding, int timeout = 30000, string userAgent = "", CookieCollection cookies = null)
{
string content = "";
HttpWebResponse myResponse;
try
{
myResponse = CreateGetHttpResponse(url, timeout, userAgent, cookies);
}
catch (WebException ex)
{
myResponse = (HttpWebResponse)ex.Response;
}
if (myResponse != null)
{
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), readCoding);
content = reader.ReadToEnd();
}
return content;
}
#endregion
不能直接用IO吧,这是我写的下载网页的。。最后的StreamReader 你简单改一下,存成别的类型也行。
真是学以至用! System.IO是读取系统文件的
网站的网址,需要用http的库下载后再保存到电脑上,用HttpClient或者其他的库都行 看来楼主用易语言纠结了好一段时间,决定重回经典{:301_1007:}
页:
[1]