📄 download程序c#源码,支持断点续传.txt
字号:
class Downfile
{
public Downfile(string url,string username,string userpwd,string savename,string proxyip,string proxyport)
{
Durl = url;
Dusername = username;
Duserpwd = userpwd;
Dsavename = savename;
Dproxyip = proxyip;
Dproxyport = proxyport;
}
public Downfile(string url, string username, string userpwd, string savename)
{
Durl = url;
Dusername = username;
Duserpwd = userpwd;
Dsavename = savename;
}
public void Work()//download file no matter the file is exist or not
{
try
{
Uri uri = new Uri(Durl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
if (!(string.IsNullOrEmpty(Dproxyip) || string.IsNullOrEmpty(Dproxyport)))
{
WebProxy myproxy = new WebProxy(Dproxyip, Convert.ToInt32(Dproxyport));
myproxy.BypassProxyOnLocal = true;
request.Proxy = myproxy;
}
NetworkCredential credentials = new NetworkCredential(Dusername, Duserpwd);
request.Credentials = credentials;
WebResponse v = request.GetResponse();
Stream rStream = v.GetResponseStream();
byte[] buffer = new byte[4096];
Stream outStream = File.Create(Dsavename);
int len;
do
{
len = rStream.Read(buffer, 0, buffer.Length);
if (len > 0)
outStream.Write(buffer, 0, len);
} while (len > 0);
outStream.Close();
rStream.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "\n");
if (WriteLog.LOgIsFine())
WriteLog.LogWrite(ex.Message + "\n");
}
}
public void ContinueWork()//if the file is existed, The downloading may continue from the point of interruption
{
if (!File.Exists(Dsavename))
{
this.Work();
return;
}
// FileInfo existfileinfo = new FileInfo(Dsavename);
FileStream fs= File.OpenWrite(Dsavename);
long lStartPos=fs.Length;
fs.Seek(lStartPos,SeekOrigin.Current);
try
{
Uri uri = new Uri(Durl);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
if (!(string.IsNullOrEmpty(Dproxyip) || string.IsNullOrEmpty(Dproxyport)))
{
WebProxy myproxy = new WebProxy(Dproxyip, Convert.ToInt32(Dproxyport));
myproxy.BypassProxyOnLocal = true;
request.Proxy = myproxy;
}
NetworkCredential credentials = new NetworkCredential(Dusername, Duserpwd);
request.Credentials = credentials;
if (lStartPos > 0)
request.AddRange((int)lStartPos);
WebResponse v = request.GetResponse();
Stream rStream = v.GetResponseStream();
byte[] buffer = new byte[4096];
// Stream outStream = File.Create(Dsavename);
int len;
do
{
len = rStream.Read(buffer, 0, buffer.Length);
if (len > 0)
fs.Write(buffer, 0, len);
} while (len > 0);
fs.Close();
rStream.Close();
}
catch (Exception ex)
{
fs.Close();
Console.WriteLine(ex.Message + "\n");
if (WriteLog.LOgIsFine())
WriteLog.LogWrite(ex.Message + "\n");
}
}
private string Durl;
private string Dusername;
private string Duserpwd;
private string Dsavename;
private string Dproxyip = "";
private string Dproxyport = null;
}
static class WriteLog
{
public static void LogOpen(string filename)
{
logsw = new StreamWriter(filename, true, System.Text.Encoding.UTF8);
logsw.AutoFlush = true;
}
public static void LogWrite(string contents)
{
logsw.Write(contents);
}
public static void LogClose()
{
logsw.Flush();
logsw.Close();
}
public static bool LOgIsFine()
{
if (logsw == null)
return false;
return true;
}
private static StreamWriter logsw = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -