Skip to main content
2 of 3
quickly modify the question.
Marc-Andre
  • 6.7k
  • 5
  • 38
  • 65

Can you critic my Download Function?

I wrote a Download Function and I wanted to know is my code clean, readable and maintainable? Or could I make things easier ? Please tell me if this Function could be use in the real world?

     public static void Download(Uri _downloadUri, string _path, string _passedSong, Song _currentObj) {

            if (_downloadUri != null) {

                if (!string.IsNullOrEmpty(_path) && !string.IsNullOrEmpty(_passedSong)) {

                    try {

                        var downloadedData = new byte[0];

                        var webReq = WebRequest.Create(_downloadUri);

                        using (var webResponse = webReq.GetResponse()) {

                            using (var dataStream = webResponse.GetResponseStream()) {

                                //Download chuncks
                                byte[] dataBuffer = new byte[1024];

                                //Get size
                                int dataLength = (int) webResponse.ContentLength;

                                ByteArgs byteArgs = new ByteArgs();

                                byteArgs.downloaded = 0;
                                byteArgs.total = dataLength;

                                if (_bytesDownloaded != null) {
                                    _bytesDownloaded(byteArgs, _currentObj);
                                }

                                //Download
                                using (var memoryStream = new MemoryStream()) {

                                    while (true) {

                                        int bytesFromStream = dataStream.Read(dataBuffer, 0, dataBuffer.Length);

                                        if (bytesFromStream == 0) {

                                            byteArgs.downloaded = dataLength;
                                            byteArgs.total = dataLength;

                                            if (_bytesDownloaded != null) {
                                                _bytesDownloaded(byteArgs, _currentObj);
                                            }

                                            //Download complete
                                            break;
                                        }
                                        else {

                                            //Write the downloaded data
                                            memoryStream.Write(dataBuffer, 0, 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");
                    }
                }
            }
        }
iNCEPTiON_
  • 317
  • 2
  • 9