IOException in doing Downloading File

  • Thread starter Thread starter Bsiang
  • Start date Start date
B

Bsiang

Good day,

I am creating PPC application which can download file from server.
But I get IOEception.

The Same code run no error in Desktop Application. Could you all point me
out ?

I am using .Net Compact Framework 1.0 SP3

Using the Code below :

private bool DownloadFile(string url, string filename)
{
HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
httpWebRequest.AllowWriteStreamBuffering = true;

if ( httpWebRequest != null )
{
HttpWebResponse httpWebResponse = (HttpWebResponse)
httpWebRequest.GetResponse();

if ( httpWebResponse != null )
{
Stream httpStream = httpWebResponse.GetResponseStream();
Stream fileStream = File.Create(filename);

if ( httpStream != null && fileStream != null ) ===>
System.IO.IOException happen here
{
byte[] buffer = new byte[1024];
int bytesRead;

do
{
bytesRead = httpStream.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, bytesRead);
}while (bytesRead > 0);

return true;
}
}
}

return false;
}


Thank you in all advance.


Best regards,
Bsiang.
 
The IOException throws File.Create method. It is possible that filename
that you passing to File.Create was readonly. Try to modify your
function in this manner:

if (File.Exists(filename)) File.Delete(filename);
after that goes File.Create

And one more thing:- don't forget to close Streams: httpStream, fileStream.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
By the way, IOException could also be thrown because you did not close
your "filename" (again, don't forget to close streams)...

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com


Sergey said:
The IOException throws File.Create method. It is possible that filename
that you passing to File.Create was readonly. Try to modify your
function in this manner:

if (File.Exists(filename)) File.Delete(filename);
after that goes File.Create

And one more thing:- don't forget to close Streams: httpStream, fileStream.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com

Good day,

I am creating PPC application which can download file from server.
But I get IOEception.

The Same code run no error in Desktop Application. Could you all point
me out ?

I am using .Net Compact Framework 1.0 SP3

Using the Code below :

private bool DownloadFile(string url, string filename)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)
WebRequest.Create(url);
httpWebRequest.AllowWriteStreamBuffering = true;

if ( httpWebRequest != null )
{
HttpWebResponse httpWebResponse = (HttpWebResponse)
httpWebRequest.GetResponse();

if ( httpWebResponse != null )
{
Stream httpStream = httpWebResponse.GetResponseStream();
Stream fileStream = File.Create(filename);

if ( httpStream != null && fileStream != null ) ===>
System.IO.IOException happen here
{
byte[] buffer = new byte[1024];
int bytesRead;

do
{
bytesRead = httpStream.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, bytesRead);
}while (bytesRead > 0);

return true;
}
}
}

return false;
}


Thank you in all advance.


Best regards,
Bsiang.
 
And telling us what other information the IoException reports would help us
tell you what's going on. Use the debugger, also, and tell us what *line*
of code is causing the problem.

Paul T.

Sergey Bogdanov said:
By the way, IOException could also be thrown because you did not close
your "filename" (again, don't forget to close streams)...

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com


Sergey said:
The IOException throws File.Create method. It is possible that filename
that you passing to File.Create was readonly. Try to modify your function
in this manner:

if (File.Exists(filename)) File.Delete(filename);
after that goes File.Create

And one more thing:- don't forget to close Streams: httpStream,
fileStream.

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com

Good day,

I am creating PPC application which can download file from server.
But I get IOEception.

The Same code run no error in Desktop Application. Could you all point
me out ?

I am using .Net Compact Framework 1.0 SP3

Using the Code below :

private bool DownloadFile(string url, string filename)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)
WebRequest.Create(url);
httpWebRequest.AllowWriteStreamBuffering = true;

if ( httpWebRequest != null )
{
HttpWebResponse httpWebResponse = (HttpWebResponse)
httpWebRequest.GetResponse();

if ( httpWebResponse != null )
{
Stream httpStream = httpWebResponse.GetResponseStream();
Stream fileStream = File.Create(filename);

if ( httpStream != null && fileStream != null ) ===>
System.IO.IOException happen here
{
byte[] buffer = new byte[1024];
int bytesRead;

do
{
bytesRead = httpStream.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, bytesRead);
}while (bytesRead > 0);

return true;
}
}
}

return false;
}


Thank you in all advance.


Best regards,
Bsiang.
 
Back
Top