我正在將一張圖片從探索畫面移至勝出表格。這工作正常。我已經移動圖片後,我想刪除它在文件夾中,但不起作用。我收到文件在winform中使用的錯誤。拖放後無法刪除文件
我曾嘗試用:
File.Delete(files[0])
files = null
img = null
img.Dispose()
但我還是不能刪除或移動文件。
private void frmADManager_DragDrop(object sender, DragEventArgs e)
{
try
{
int x = PointToClient(new Point(e.X, e.Y)).X;
int y = PointToClient(new Point(e.X, e.Y)).Y;
if (x >= pbUser.Location.X && x <= pbUser.Location.X + pbUser.Width && y >= pbUser.Location.Y && y <= pbUser.Location.Y + pbUser.Height)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
Image img = Image.FromFile(files[0]);
if (img.Width == 648)
{
pbUser.Image = img;
SavePicture = true;
tsbtnSave.Enabled = true;
toolStrip1.Focus();
File.Delete(files[0]);
files = null;
img = null;
img.Dispose();
}
else
如果您需要保留圖像中'pbUser'那麼你應該使用的源代碼的副本。 I.E .:'pbUser.Image =新的位圖(img);'。也請遵循@waka的回答。不要試圖使用'img.Clone()',因爲克隆還會引用源文件並阻止您刪除該文件。 – TnTinMn