Я не очень опытный программист, поэтому я попросил ChatGPT дать ответы, но, похоже, ничего из этого не работает.Для подробностей: я успешно выполнил функцию сохранения на подключенный диск внутри «Серверного» ПК, и теперь я хочу создать функцию удаления. Каждый код, который я пробовал, выдает мне эту ошибку: «Процесс не может получить доступ к файлу, поскольку он используется другим процессом».
Мне не нужна эта ошибка, поскольку в этом приложении будет несколько пользователи и я хотим, чтобы они свободно удаляли любые изображения, сохраняемые на ПК.
Может ли кто-нибудь дать ответ на этот вопрос? Любая помощь приветствуется. Вот код.
private void DeletePic_Click(object sender, EventArgs e)
{
// Сообщение с подтверждением
DialogResult result = MessageBox.Show( «Хотите удалить это изображение?», «Подтвердить удаление», MessageBoxButtons.ДаНет, MessageBoxIcon.Вопрос);
Код: Выделить всё
if (result == DialogResult.Yes)
{
try
{
string Appcode = ItemCode.Text;
string appPath = @"\\IP.ADDRESS\SCM_Master_Documents\AppPicture";
string picPath = Path.Combine(appPath, Appcode + ".jpg");
string tempPath = Path.Combine(appPath, Appcode + "_temp.jpg");
// Release resources if the picture is currently loaded
if (EmpPic.Image != null)
{
EmpPic.Image.Dispose();
EmpPic.Image = null;
}
// Check if the file exists
if (File.Exists(picPath))
{
// Attempt to move the file to a temporary location
File.Move(picPath, tempPath);
// Retry logic to delete the renamed file
int maxRetries = 3;
int delay = 2000; // Increase delay to 2 seconds
for (int i = 0; i < maxRetries; i++)
{
try
{
// Wait before retrying
System.Threading.Thread.Sleep(delay);
// Delete the file
File.Delete(tempPath);
MessageBox.Show("Picture deleted successfully.");
break; // Exit loop if deletion successful
}
catch (IOException ioEx) when (i < maxRetries - 1)
{
// Log or handle the exception if necessary
Debug.WriteLine($"Attempt {i + 1}: Failed to delete file. {ioEx.Message}");
}
}
}
else
{
MessageBox.Show("Picture not found.");
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while deleting the picture: " + ex.Message);
}
}
else
{
MessageBox.Show("Picture deletion canceled.");
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... pplication
Мобильная версия