该进程无法访问该文件' '因为它正在被另一个进程使用
我正在尝试删除使用文件对话框上传的图像文件的本地副本(在计算机上)。它抛出进程无法访问文件“C:\ Documents and Settings \用户名\我的文档\我的图片\ 1220.bmp”,因为它正在被另一个进程使用。
private void _btnImportPhoto_Click(object sender, RoutedEventArgs e)
{
//user clicked import/change photo, open file dialog to browse for photo
System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
fileDialog.Multiselect = false;
fileDialog.Filter = ResourceFile.PhotoFileTypes;
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FilePath = fileDialog.FileName;
FilePathCopy = fileDialog.FileName;
string safeFilePath = fileDialog.SafeFileName;
Bitmap bitmap = new Bitmap(FilePath);
CurrentPhoto = bitmap;
Bitmap bitmap1 = new Bitmap(FilePathCopy); //A copy to save when if delete local copy is chosen
m_PhotoCopy = bitmap1;
FileSystem.DeleteFile(FilePath);
}
_btnSave.IsEnabled = _btnCancel.IsEnabled = true;
}
}
请让我知道如何解决此问题。 谢谢。
I am trying to delete local copy(on the computer) of an image file once uploaded using file dialog. It throws The process cannot access the file 'C:\Documents and Settings\username\My Documents\My Pictures\1220.bmp' because it is being used by another process.
private void _btnImportPhoto_Click(object sender, RoutedEventArgs e)
{
//user clicked import/change photo, open file dialog to browse for photo
System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
fileDialog.Multiselect = false;
fileDialog.Filter = ResourceFile.PhotoFileTypes;
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FilePath = fileDialog.FileName;
FilePathCopy = fileDialog.FileName;
string safeFilePath = fileDialog.SafeFileName;
Bitmap bitmap = new Bitmap(FilePath);
CurrentPhoto = bitmap;
Bitmap bitmap1 = new Bitmap(FilePathCopy); //A copy to save when if delete local copy is chosen
m_PhotoCopy = bitmap1;
FileSystem.DeleteFile(FilePath);
}
_btnSave.IsEnabled = _btnCancel.IsEnabled = true;
}
}
Please let me know how to work around this issue.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要处理位图对象尝试这样做。因为这将在位图对象离开使用上下文 { } 后立即对其进行处理
you need to dispose of the bitmap object try doing this. As this will dispose of the bitmap object as soon as it leaves the using context { }
首先,我看到
CurrentPhoto
我假设您想要保存一些全局变量。相反,这会引发异常:
导致
FilePath
处的图像文件实际上是CurrentPhoto
。你能做什么。1)如果使用
CurrentPhoto
在此函数内有任何意义,请在该函数内执行您想要执行的操作,并在处置CurrentPhoto
对象之后,即使以@Bobby 之类的方式suggest (using
block)2)如果你想顺便拥有的话,可以尝试使用Bitmap的克隆
像这样的方法:
并且在调用您之后:
应该有效。
First I see
Where
CurrentPhoto
I presume some global variable that you want hold.This, instead throws and exception:
Cause image file at
FilePath
is actuallyCurrentPhoto
. What you can do.1) If use of
CurrentPhoto
has any sense inside this function, do what you want to do inside this function, and after disposeCurrentPhoto
object, even in a way like @Bobby suggests (using
block)2) If you want to have it by the way, you can try to use Bitmap's Clone
method like this:
and after call your:
Should work.
试试这个...
http://www.lockergnome.com/blade/2006/11/28/windows-error-message-error-deleting-file-or-folder/
它将允许您删除文件或文件夹。
Try this...
http://www.lockergnome.com/blade/2006/11/28/windows-error-message-error-deleting-file-or-folder/
It will allow you to delete files or folder.