Note that there are some explanatory texts on larger screens.

plurals
  1. POError while closing ftp stream in C#
    text
    copied!<p>I'm trying to cancel a downloading operation. My scenario is as follows:</p> <p>When the user clicks on Cancel Download Button so this action throws exception in Download function which is as follows:</p> <pre><code> try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + uri + "/" + fileName)); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.UsePassive = true; response = (FtpWebResponse)reqFTP.GetResponse(); ftpStream = response.GetResponseStream(); _isItOutputStream = true; string dataLengthString = response.Headers["Content-Length"]; int dataLength = 0; if (dataLengthString != null) { dataLength = Convert.ToInt32(dataLengthString); } long cl = response.ContentLength; int bufferSize = 4048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); bool first = true; while (readCount &gt; 0) { outputStream.Write(buffer, 0, readCount); _actualDownloaded += readCount; if (this.InvokeRequired) { ProgressBarDel _progressDel = new ProgressBarDel(ProgressBar); this.Invoke(_progressDel, new object[] { _actualDownloaded, first }); } first = false; readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); _isItOutputStream = false; return true; } catch (Exception ee) { _downloadException = ee.Message; if (response != null) { outputStream.Close(); ftpStream.Close(); response.Close(); } return false; } </code></pre> <p>In the line " <code>ftpStream.Close()</code> " here where the exception is thrown...</p> <p>The exception text is:</p> <blockquote> <p>The remote server returned an error: (450) File unavailable (e.g., file busy) </p> </blockquote> <p>where it opens a file to download as what i wrote " <code>outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);</code>" i wont to close this stream and to close the response in order if the user did as follows: download -> cancel download -> download -> cancel download -> download </p> <p>if this scenario happened the application hungs up. i don't know how to close the stream and the response so i can stop downloading then delete the created file in order to download again.</p> <p>thnx</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload