我开发了一个C#应用程序,在应用程序中用户为每条记录选择一张照片。但是,用户也应该能够使用较新的照片更改预选照片。当用户更改照片时,应用程序首先从应用程序目录中删除旧照片,然后复制新照片,但是当应用程序执行该操作时会发出异常,因为应用程序使用该文件,因此应用程序不能删除运行。有没有人有一个线索如何解决这个问题? 我感谢你的帮助如何在应用程序运行时删除文件
这是例外
该进程无法访问该文件 “d:\我的 项目\ Hawkar'sProject \ SOFTWARE \应用\ BIN \调试\照片\ John Smith.png',因为它正在被另一个进程 使用。
//defining a string where contains the file source path
string fileSource = Open.FileName;
//defining a string where it contains the file name
string fileName = personNameTextBox.Text + ".png" ;
//defining a string which specifies the directory of the destination file
string fileDest = dir + @"\Photos\" + fileName;
if (File.Exists(fileDest))
{
File.Delete(fileDest);
//this is a picturebox for showing the images
pbxPersonal.Image = Image.FromFile(dir + @"\Photos\" + "No Image.gif");
File.Copy(fileSource, fileDest);
}
else
{
File.Copy(fileSource, fileDest);
}
imageIDTextBox.Text = fileDest;
“嗨男装” - 可能不仅有男性在看> ;-) – smirkingman 2010-11-30 13:51:54