Only writing 1 byte to a file stream on one pc in office

C

Claire

Hi,
I read image data from a database and cache them locally to file via a
filestream.
One of my users commented that his application isnt showing images. On
checking his file cache I found that most of the files were only 1 byte in
length. I deleted all the files, re-ran the application and again the files
were truncated after 1 byte.
I protect my code with try/catch and log all exceptions to the windows event
log and to a log file. There were no exceptions listed.
We're on a domain.
The code works fine on other pcs. I can't work out why only one byte, or why
there's no error

// read in the record with binary reader
tableImages datarecord = new tableImages(reader);
// write the image to file
FileStream fs = new FileStream(ImageLocation, FileMode.Create,
FileAccess.Write, FileShare.None);
try
{
// Assign the image bytes to variable
byte[] data = datarecord.FileData;
fs.Write(data, 0, data.Length);
fs.Flush();
}
finally
{
fs.Close();
}// finally
 
G

Guest

In this instance though you are not catching and logging anything. You are
just ignoring any issues and closing the open stream.
 
P

Peter Duniho

[...]
One of my users commented that his application isnt showing images. On
checking his file cache I found that most of the files were only 1 byte
in
length. I deleted all the files, re-ran the application and again the
files
were truncated after 1 byte.

Well, the code you posted doesn't seem to have any obvious problems. So,
clearly you haven't posted all of the interesting code. For example, what
does the tableImages.FileData property do?

I prefer the "using" syntax rather than a try/catch/finally without a
catch...seems "nicer" to me. But I don't think that should affect your
issue.

I would say that either the data length in your scenario is actually 1
byte, or something is going wrong when you write it. You should debug the
code to get more information about what is actually happening. Also,
maybe you should check the FileStream.Position property to see just how
many bytes were really written.

If you want more help from here, you should post a concise-but-complete
sample of code that reliably reproduces the problem. Don't post more code
than is minimally required to reproduce the problem.

It's likely you'll find the error simply by creating the sample, but if
not then at least you'll have something you can post here and which others
can practically use to try to help you with the problem.

Pete
 
B

Ben Voigt [C++ MVP]

ModelBuilder said:
In this instance though you are not catching and logging anything. You
are
just ignoring any issues and closing the open stream.

???

The OP has a finally block, not catch (...), so he isn't ignoring anything.
His application-level exception handler will log any exception... but the
whole call stack should be checked for silent discards.
Claire said:
Hi,
I read image data from a database and cache them locally to file via a
filestream.
One of my users commented that his application isnt showing images. On
checking his file cache I found that most of the files were only 1 byte
in
length. I deleted all the files, re-ran the application and again the
files
were truncated after 1 byte.
I protect my code with try/catch and log all exceptions to the windows
event
log and to a log file. There were no exceptions listed.
We're on a domain.
The code works fine on other pcs. I can't work out why only one byte, or
why
there's no error

// read in the record with binary reader
tableImages datarecord = new tableImages(reader);
// write the image to file
FileStream fs = new FileStream(ImageLocation, FileMode.Create,
FileAccess.Write, FileShare.None);
try
{
// Assign the image bytes to variable
byte[] data = datarecord.FileData;
fs.Write(data, 0, data.Length);
fs.Flush();
}
finally
{
fs.Close();
}// finally
 

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