I wrote a Download Functiondownload function and I wanted to know isif my code clean, readable and maintainable? Or could, or if I could make things easier ?. Please tell me if this Functionfunction could be useused in the real world?.
public static void Download(Uri _downloadUri, string _path, string _passedSong, Song _currentObj) {
if (_downloadUri != null) {
if (!string.IsNullOrEmpty(_path) &&_downloadUri !string.IsNullOrEmpty(_passedSong)= null) {
if (!string.IsNullOrEmpty(_path) && try!string.IsNullOrEmpty(_passedSong)) {
var downloadedData = newtry byte[0];{
var webReqdownloadedData = WebRequest.Create(_downloadUri);new byte[0];
using (var webResponsewebReq = webReqWebRequest.GetResponseCreate()_downloadUri) {;
using (var dataStreamwebResponse = webResponsewebReq.GetResponseStreamGetResponse()) {
//Download chuncks
using byte[](var dataBufferdataStream = newwebResponse.GetResponseStream()) byte[1024];{
//GetDownload sizechuncks
intbyte[] dataLengthdataBuffer = (int)new webResponse.ContentLength;byte[1024];
//Get size
ByteArgs byteArgs = new ByteArgs int dataLength = (int); webResponse.ContentLength;
ByteArgs byteArgs.downloaded = 0;
byteArgs.total =new dataLength;ByteArgs();
if (_bytesDownloadedbyteArgs.downloaded != null) {0;
_bytesDownloaded(byteArgs, _currentObj);
.total = }dataLength;
if (_bytesDownloaded != null) //Download{
using _bytesDownloaded(varbyteArgs, memoryStream_currentObj);
= new MemoryStream()) { }
//Download
while using (truevar memoryStream = new MemoryStream()) {
int bytesFromStream =while dataStream.Read(dataBuffer, 0, dataBuffer.Lengthtrue); {
int bytesFromStream if= dataStream.Read(bytesFromStream ==dataBuffer, 0), {dataBuffer.Length);
byteArgs.downloaded = dataLength;
if (bytesFromStream byteArgs.total== =0) dataLength;{
if (_bytesDownloadedbyteArgs.downloaded != null) {dataLength;
_bytesDownloaded(byteArgs, _currentObj);
.total = }dataLength;
if (_bytesDownloaded != //Downloadnull) complete{
break;_bytesDownloaded(byteArgs, _currentObj);
}
else {
//Download complete
//Write the downloaded data break;
}
memoryStream.Write(dataBuffer, 0, bytesFromStream); else {
//Write the downloaded data
byteArgs memoryStream.downloadedWrite(dataBuffer, =0, bytesFromStream;bytesFromStream);
byteArgs.downloaded = bytesFromStream;
byteArgs.total = dataLength;
// for download progressbar
if (_bytesDownloaded != null) {
_bytesDownloaded(byteArgs, _currentObj);
}
}
}
}
//Convert to byte array
downloadedData = memoryStream.ToArray();
//Write bytes to the specified file
if (Directory.Exists(Settings.Default.downloadDirectory)) {
using (var newFile = new FileStream(_path, FileMode.Create)) {
newFile.Write(downloadedData, 0, downloadedData.Length);
}
}
}
}
}
}
catch(Exception ex) {
ExceptionLog.LogException(ex, "Download()", "in GeneralSettings.cs");
}
}
catch(Exception ex) {
ExceptionLog.LogException(ex, "Download()", "in GeneralSettings.cs");
}
}
}
}