IOException in doing Downloading File

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.
 
S

Sergey Bogdanov

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
 
S

Sergey Bogdanov

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.
 
P

Paul G. Tobey [eMVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top