Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple function in C# to download a file from the internet, supports to resume the download.
public static void downloadFile(string sourceURL, string destinationPath)
{
long fileSize = 0;
int bufferSize = 1024;
bufferSize *= 1000;
long existLen = 0;
System.IO.FileStream saveFileStream;
if (System.IO.File.Exists(destinationPath))
{
System.IO.FileInfo destinationFileInfo = new System.IO.FileInfo(destinationPath);
existLen = destinationFileInfo.Length;
}
if (existLen > 0)
saveFileStream = new System.IO.FileStream(destinationPath,
System.IO.FileMode.Append,
System.IO.FileAccess.Write,
System.IO.FileShare.ReadWrite);
else
saveFileStream = new System.IO.FileStream(destinationPath,
System.IO.FileMode.Create,
System.IO.FileAccess.Write,
System.IO.FileShare.ReadWrite);
System.Net.HttpWebRequest httpReq;
System.Net.HttpWebResponse httpRes;
httpReq = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(sourceURL);
httpReq.AddRange((int) existLen);
System.IO.Stream resStream;
httpRes = (System.Net.HttpWebResponse) httpReq.GetResponse();
resStream = httpRes.GetResponseStream();
fileSize = httpRes.ContentLength;
int byteSize;
byte[] downBuffer = new byte[bufferSize];
while ((byteSize = resStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
saveFileStream.Write(downBuffer, 0, byteSize);
}
}
@NamuTree0345

This comment has been minimized.

Copy link

@NamuTree0345 NamuTree0345 commented Mar 13, 2020

Called the UnauthorizedAccessException in 21 line
How to fix it?

@maythamfahmi

This comment has been minimized.

Copy link

@maythamfahmi maythamfahmi commented Apr 7, 2020

Called the UnauthorizedAccessException in 21 line
How to fix it?

I guess you pass folder name without filename.

This give this kind of error => C:\myfolder\

This works fine => C:\myfolder\myfile.ext

@bstiffler582

This comment has been minimized.

Copy link

@bstiffler582 bstiffler582 commented Apr 29, 2020

You need to close the stream with saveFileStream.Close() at the end of your function. Else if you try to use the file immediately after downloading, you will get an exception.

@Mariogamerexpert

This comment has been minimized.

Copy link

@Mariogamerexpert Mariogamerexpert commented Jun 9, 2020

Hi, I'm really new to C# (this is like my first time using it) I'm trying to make a C# commandline app and one of the commands I'm trying to make is a "checkforupdates" command which then checks one of my private repositories for a filename similar to my commandline app's filename but with a higher version number (so say for example Alpha 1 detects Alpha 2 and it's ready to download it, but if Alpha 1 detects Alpha 1 it doesn't download it). Can you please help me with this?

@maythamfahmi

This comment has been minimized.

Copy link

@maythamfahmi maythamfahmi commented Jun 11, 2020

Hi, I'm really new to C# (this is like my first time using it) I'm trying to make a C# commandline app and one of the commands I'm trying to make is a "checkforupdates" command which then checks one of my private repositories for a filename similar to my commandline app's filename but with a higher version number (so say for example Alpha 1 detects Alpha 2 and it's ready to download it, but if Alpha 1 detects Alpha 1 it doesn't download it). Can you please help me with this?

please spend some time learning c# and you will find a solution.

@NamuTree0345

This comment has been minimized.

Copy link

@NamuTree0345 NamuTree0345 commented Jun 16, 2020

Called the UnauthorizedAccessException in 21 line
How to fix it?

I guess you pass folder name without filename.

This give this kind of error => C:\myfolder\

This works fine => C:\myfolder\myfile.ext

Thank you! It's worked!

@Medard-prog

This comment has been minimized.

Copy link

@Medard-prog Medard-prog commented Feb 22, 2021

System.UnauthorizedAccessException: 'Access to the path 'F:\asd' is denied.'

Please help me!

@maythamfahmi

This comment has been minimized.

Copy link

@maythamfahmi maythamfahmi commented Feb 22, 2021

System.UnauthorizedAccessException: 'Access to the path 'F:\asd' is denied.'

Please help me!

This is not related to code, the code telling you that you do not have permission on F drive with the given folder name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment