文件正被另一个进程使用
private void ButtonCustomarinfoEditClick(object sender, System.Windows.RoutedEventArgs e)
{
ByteToImage(fileName,bytesOfImage,fileSize);
}
private ImageSource ByteToImage(string fileName, byte[] bytesOfImage, int fileSize)
{
FileStream imageFilestream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
imageFilestream.Write(bytesOfImage, 0, fileSize);
imageFilestream.Flush();
imageFilestream.Close();
imageFilestream.Dispose();
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(fileName);
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
return myBitmapImage;
}
当我第一次单击 ButtonCustomarinfoEdit 时,它工作正常。但是当我第二次单击时,它会抛出此异常
捕获:“该进程无法访问文件'C:\ 20.jpg',因为它正在被另一个进程使用。” (系统.IO.IOException)
异常消息 =“该进程无法访问文件“C:\20.jpg”,因为该文件正在被另一个进程使用。”,异常类型 =“System.IO.IOException”
private void ButtonCustomarinfoEditClick(object sender, System.Windows.RoutedEventArgs e)
{
ByteToImage(fileName,bytesOfImage,fileSize);
}
private ImageSource ByteToImage(string fileName, byte[] bytesOfImage, int fileSize)
{
FileStream imageFilestream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
imageFilestream.Write(bytesOfImage, 0, fileSize);
imageFilestream.Flush();
imageFilestream.Close();
imageFilestream.Dispose();
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(fileName);
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
return myBitmapImage;
}
When i click ButtonCustomarinfoEdit fist time then it work`s fine. But When i click second time then it throw this exception
Caught: "The process cannot access the file 'C:\20.jpg' because it is being used by another process." (System.IO.IOException)
Exception Message = "The process cannot access the file 'C:\20.jpg' because it is being used by another process.", Exception Type = "System.IO.IOException"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 BitmapImage 对象使文件保持锁定状态。
只是一个小观察,请使用
using
语句 像这样:否则你可能会遇到文件将继续使用的情况(如果在调用 Close() 方法之前发生异常。
Your BitmapImage object keeps the file locked.
Just a small observation, please use the
using
statement like this:otherwise you might run into situations that your file will remain in use (if an exception will occur before you call the Close() method.