文件流错误... [英] Filestream Error...

查看:51
本文介绍了文件流错误...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个产品列表,其中包含存储在SQL Server 2000 DB中的图像。我需要能够在选择产品时检索图像。我使用下面的

代码从数据库中获取图像并将其分配到图片框。


它第一次正常工作但后来它很好给我一个错误

说该文件正由另一个进程使用。什么过程?如果我去

文件夹并尝试删除Windows中的文件,我会得到同样的错误。可以

有人帮我吗?我究竟做错了什么?我正在关闭文件流所以

什么是开放的?


谢谢


Tim
< br $> b $ b试试


{


//查看图片


SqlCommand cmdSelect = new SqlCommand(" SELECT logo_image FROM

t_store_information WHERE store_id = 1",DBConnection.DBConn.Connection);


DBConnection.DBConn。 Connection.Open();


byte [] barrImg =(byte [])cmdSelect.ExecuteScalar();


DBConnection.DBConn。 Connection.Close();


string strfn = Convert.ToString(DateTime.Now.ToFileTime());


FileStream fs = new FileStream(@C:\Program Files \ MM\Temp\thumb.jpg",

FileMode.OpenOrCreate,FileAccess.Write);


fs.Write(barrImg,0,barrImg.Length);


fs.Close();


strfn =" " ;;


this.pboxThumb.Image = Image.FromFile(@" C:\Program

Files \ MM\Temp\thumb.jpg");


}


catch(Exception ex)


{


#if DEBUG


MessageBox.Show(" Image Error!\ r\\\
\r\\\
" + ex.Message);


#endif


}


终于


{


DBConnection.DBConn.Connection.Close();


}

Hi,

I have a list of products in with images stored in a SQL Server 2000 DB. I
need to be able to retrieve the images when a product is chose. I use the
code below to get the image from the DB and assign it to the picture box.

It works fine the first time but subsequent times it gives me an error
saying that the file is in use by another process. What process? If I go to
the folder and try to delete the file in windows I get the same error. Can
anyone help me here? What am I doing wrong? I am closing the filestream so
what is still open?

Thanks

Tim

try

{

//Look Up the Image

SqlCommand cmdSelect=new SqlCommand("SELECT logo_image FROM
t_store_information WHERE store_id =1" , DBConnection.DBConn.Connection);

DBConnection.DBConn.Connection.Open();

byte[] barrImg=(byte[])cmdSelect.ExecuteScalar();

DBConnection.DBConn.Connection.Close();

string strfn=Convert.ToString(DateTime.Now.ToFileTime());

FileStream fs = new FileStream(@"C:\Program Files\MM\Temp\thumb.jpg",
FileMode.OpenOrCreate, FileAccess.Write);

fs.Write(barrImg,0,barrImg.Length);

fs.Close();

strfn = "";

this.pboxThumb.Image = Image.FromFile(@"C:\Program
Files\MM\Temp\thumb.jpg");

}

catch(Exception ex)

{

#if DEBUG

MessageBox.Show("Image Error!\r\n\r\n" + ex.Message);

#endif

}

finally

{


DBConnection.DBConn.Connection.Close();

}

推荐答案

你好蒂姆,


如果我是你,我不会使用FileStream而是使用MemoryStream。就这样

你可以避免所有那些粘贴文件的问题,它会为你做一个好的交易

更快。


记录可能发生的事情是FromFile函数

实现可能不会释放非托管的FileHandle快速
足够但我无法确定。


如果你想查看哪个进程持有文件句柄,请尝试使用

来自 www.sysinternals.com


祝你好运,

Jan

Hi Tim,

If I were you I wouldn''t use a FileStream but a MemoryStream. That way
you avoid all those stick file issues and it will work a goof deal
faster for you.

For the record what might be happeneing is that the FromFile functions
implementation may not be releasnig the unmanaged FileHandle fast
enough but i can''t be sure.

If you want to see what process is holding the file handle try using
Process Explorer from www.sysinternals.com.

Good Luck,
Jan


谢谢Jan,


我不觉得你有一个MemoryStream的例子吗?另外我认为

图片盒必须有一个实际存在于硬

驱动器上的文件。


Tim

" Jan" < JA *********** @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
Thanks Jan,

I don''t suppose you have an example of MemoryStream do you? Also I thought
that pictureboxes had to have a file that actually existed on the hard
drive.

Tim
"Jan" <ja***********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
蒂姆,

如果我是你,我不会使用FileStream而是使用MemoryStream。那样你就可以避免所有那些粘贴文件问题了,它会更快地为你做好准备。

记录可能发生的事情是FromFile函数
实现可能不会释放非托管的FileHandle足够快,但我不能确定。

如果你想查看哪个进程持有文件句柄,请尝试使用
来自 www.sysinternals.com 的Process Explorer。

祝你好运,
Jan
Hi Tim,

If I were you I wouldn''t use a FileStream but a MemoryStream. That way
you avoid all those stick file issues and it will work a goof deal
faster for you.

For the record what might be happeneing is that the FromFile functions
implementation may not be releasnig the unmanaged FileHandle fast
enough but i can''t be sure.

If you want to see what process is holding the file handle try using
Process Explorer from www.sysinternals.com.

Good Luck,
Jan



你好蒂姆,


这是一个例子:


使用(System.IO.MemoryStream mem = new

System.IO.MemoryStream(bytes,false))

{

System.Drawing.Bitmap bitmap = new Bitmap(mem,false);

pictureBox1.Image = Bitmap;

}


并且没有PictureBoxes不需要文件。他们只是和Image(这是
Bitmap的超类)。


希望有所帮助,

Jan

Hi Tim,

Here''s an example:

using (System.IO.MemoryStream mem = new
System.IO.MemoryStream(bytes,false))
{
System.Drawing.Bitmap bitmap = new Bitmap(mem,false);
pictureBox1.Image = Bitmap;
}

And no PictureBoxes don''t need a file. They just and Image (which is
the superclass of Bitmap).

Hope that helps,
Jan


这篇关于文件流错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆