Click here to Skip to main content
16,015,970 members
Home / Discussions / C#
   

C#

 
AnswerRe: Convert Console Application C# sharp Method Pin
Fayu24-Oct-08 9:10
Fayu24-Oct-08 9:10 
AnswerRe: Convert Console Application C# sharp Method Pin
zjaffary25-Oct-08 3:05
zjaffary25-Oct-08 3:05 
GeneralRe: Convert Console Application C# sharp Method Pin
Fayu12-Nov-08 15:46
Fayu12-Nov-08 15:46 
QuestionResuming a download stream Pin
Fayu24-Oct-08 8:57
Fayu24-Oct-08 8:57 
AnswerRe: Resuming a download stream Pin
Ennis Ray Lynch, Jr.24-Oct-08 9:10
Ennis Ray Lynch, Jr.24-Oct-08 9:10 
GeneralRe: Resuming a download stream Pin
Fayu24-Oct-08 9:13
Fayu24-Oct-08 9:13 
GeneralRe: Resuming a download stream Pin
Ennis Ray Lynch, Jr.24-Oct-08 9:22
Ennis Ray Lynch, Jr.24-Oct-08 9:22 
AnswerResolution: Pin
Fayu24-Oct-08 19:13
Fayu24-Oct-08 19:13 
This works the way it should. I havnt optimized it, so if you want to use this code, look it over and clean it up.
Read the comments. Suggestions are appreciated. Im working on optimizing my code. Too sleepy to do it today.
public void DownloadFile(System.Uri url, string filePath, long sz)
{
    Stream r = null;
    Stream output = GetFileStream(filePath);

    // 1. create a web request
    System.Net.WebRequest request = System.Net.WebRequest.Create(url);
    System.Net.HttpWebRequest hrequest = (System.Net.HttpWebRequest)request;

    //This is the key right here
    //This reads the file that already exists
    //and gets the length of the file
    //and resumes from where it left off
    hrequest.AddRange(Convert.ToInt32(w.Length));

    hrequest.UserAgent = "Mozilla/4.0 (compatiable; MSIE 7.0; Windows NT 6.0)";

    System.Net.WebResponse response = null;
    try
    {
        using (response = request.GetResponse())
        {
            using (r = response.GetResponseStream())
            {
                using (output)
                {
                    WriteToFile(r, output, sz, w.Length);
                }
            }
        }
    }
    catch (Exception ex)
    {
        // on error, delete the partially downloaded file
        File.Delete(filePath);
        throw new Exception("Could not save " + url, ex);
    }
    finally
    {

    }
}


private Stream GetFileStream(string filePath)
{
    string fileDirectory = filePath.Substring(0, filePath.LastIndexOf(@"\"));
    if (!Directory.Exists(fileDirectory))
        Directory.CreateDirectory(fileDirectory);

    if (File.Exists(filePath))
        return File.Open(filePath, FileMode.Append);
    else
        return File.Create(filePath);
}


private void WriteToFile(Stream response, Stream output, long sz, long initSize)
{
    DateTime startTime = DateTime.Now;

    if (sz == 0)
        return;

    byte[] buffer = new byte[1024 * 64];
    long read = 0;
    long totalRead = initSize;

    while ((read = response.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, Convert.ToInt32(read));
    }

    w.Flush();
}

AnswerRe: Resuming a download stream Pin
zjaffary25-Oct-08 3:06
zjaffary25-Oct-08 3:06 
GeneralRe: Resuming a download stream Pin
Guffa25-Oct-08 3:16
Guffa25-Oct-08 3:16 
QuestionPrint error from CaptureAndPrintMSChart project [modified] Pin
ATC24-Oct-08 8:40
ATC24-Oct-08 8:40 
QuestionCapture Screen problem Pin
Xmen Real 24-Oct-08 8:09
professional Xmen Real 24-Oct-08 8:09 
Questionsizing editor Pin
netJP12L24-Oct-08 7:26
netJP12L24-Oct-08 7:26 
Questiondisplaying proxyAddresses attribute on a AD account Pin
lane0p224-Oct-08 5:30
lane0p224-Oct-08 5:30 
AnswerRe: displaying proxyAddresses attribute on a AD account Pin
Parwej Ahamad24-Oct-08 7:40
professionalParwej Ahamad24-Oct-08 7:40 
QuestionCan't set HorizontalScroll.Visible attribute Pin
Alan Balkany24-Oct-08 5:19
Alan Balkany24-Oct-08 5:19 
AnswerRe: Can't set HorizontalScroll.Visible attribute Pin
DaveyM6924-Oct-08 5:57
professionalDaveyM6924-Oct-08 5:57 
GeneralRe: Can't set HorizontalScroll.Visible attribute Pin
Luc Pattyn24-Oct-08 6:46
sitebuilderLuc Pattyn24-Oct-08 6:46 
GeneralRe: Can't set HorizontalScroll.Visible attribute Pin
Alan Balkany24-Oct-08 7:08
Alan Balkany24-Oct-08 7:08 
GeneralRe: Can't set HorizontalScroll.Visible attribute Pin
DaveyM6924-Oct-08 7:42
professionalDaveyM6924-Oct-08 7:42 
GeneralRe: Can't set HorizontalScroll.Visible attribute Pin
Alan Balkany24-Oct-08 7:47
Alan Balkany24-Oct-08 7:47 
GeneralRe: Can't set HorizontalScroll.Visible attribute Pin
Giorgi Dalakishvili24-Oct-08 8:18
mentorGiorgi Dalakishvili24-Oct-08 8:18 
GeneralRe: Can't set HorizontalScroll.Visible attribute Pin
Luc Pattyn24-Oct-08 9:15
sitebuilderLuc Pattyn24-Oct-08 9:15 
GeneralRe: Can't set HorizontalScroll.Visible attribute Pin
Giorgi Dalakishvili24-Oct-08 9:22
mentorGiorgi Dalakishvili24-Oct-08 9:22 
Question[Message Deleted] Pin
tozy24-Oct-08 4:01
tozy24-Oct-08 4:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.