Файл занят другим процессом - C# (182685)
Формулировка задачи:
the process cannot access the file... because it is being used by another process
Пытаюсь удалить файлы, которые до этого добавлял в bitmapCollection и сравнивал на подобие
Листинг программы
- // ... какой то код
- while(true)
- {
- switch(selection){
- // пару case'ов
- //....
- case "compare":
- string[] filesCollection = Directory.GetFiles(mainPath, "*.png"); // path of every single photo
- if (Directory.Exists(mainPath) && filesCollection.Length >= 2)
- {
- Array.Sort(filesCollection);
- Bitmap[] bitmapCollection = new Bitmap[filesCollection.Length];
- int i = 0;
- foreach (string item in filesCollection)
- {
- bitmapCollection[i] = new Bitmap(item.ToString());
- i++;
- Console.WriteLine("{0} из {1}", i, filesCollection.Length);
- }
- // узнаем 1 процент от общего кол-ва элементов
- double percent = (double)bitmapCollection.Length / (double)100;
- // 100 %
- List<double> percentsList = new List<double>();
- for (int s = 1; s < 101; s++)
- {
- percentsList.Add(percent * s);
- }
- //
- Console.WriteLine("Коллекция фотографий успешно собрана.");
- Console.WriteLine("Начинается проверка фотографий....");
- // int value = 1;
- bool bFlag = false;
- List<TrashPaths> repeatPhotosList = new List<TrashPaths>();
- List<int> arrayX = new List<int>(); // Чтобы J не повторял уже пройденные X
- for (int x = 0; x < bitmapCollection.Length; x++)
- {
- // Console.WriteLine("Цикл {0} из {1} итераций", x, bitmapCollection.Length - 1);
- for (int j = 0; j < bitmapCollection.Length; j++)
- {
- bool isNext = false;
- if (arrayX.Count >= 1)
- {
- foreach (var arrayItem in arrayX)
- {
- if (j == arrayItem)
- {
- isNext = true;
- break;
- }
- }
- }
- if (isNext == true)
- {
- continue;
- }
- bFlag = PhotoComparing.ImageCompareArray(bitmapCollection[x], bitmapCollection[j]);
- if (bFlag == true && x != j)
- {
- ChangeColor.greenForeground();
- string TheSameX = filesCollection[x];
- string TheSameJ = filesCollection[j];
- TheSameX = TheSameX.Replace(mainPath + @"", "");
- TheSameJ = TheSameJ.Replace(mainPath + @"", "");
- repeatPhotosList.Add(new TrashPaths() { image = bitmapCollection[x], path = TheSameX });
- Console.WriteLine("Найден подобный. " + TheSameX + " похож с " + TheSameJ);
- ChangeColor.ResetColor();
- arrayX.Add(x);
- filesToDelete.Add(filesCollection[x]);
- }
- bFlag = false;
- }
- Console.WriteLine("{0} из {1} проверок", x, bitmapCollection.Length);
- /*
- //if (value < 100)
- // {
- if (((double)x >= (percentsList[value] - 2)) && ((double)x <= percentsList[value]))
- {
- ChangeColor.redForeground();
- Console.WriteLine("{0} из {1} проверок", x,j);
- value++;
- ChangeColor.ResetColor();
- }
- // }*/
- }
- Console.WriteLine();
- ChangeColor.redForeground();
- Console.WriteLine("100% Завершено");
- ChangeColor.greenForeground();
- Console.WriteLine("Копирование дубликатов...");
- ChangeColor.ResetColor();
- int kolvo = 0;
- if (!Directory.Exists(mainPath + @"\Trash"))
- {
- DirectoryInfo dir2 = new DirectoryInfo(mainPath + @"\Trash");
- dir2.Create();
- }
- foreach (var image in repeatPhotosList)
- {
- image.image.Save(string.Format(mainPath + @"\Trash\{0}.png", image.path), System.Drawing.Imaging.ImageFormat.Png);
- kolvo++;
- }
- canDelete = true;
- Console.WriteLine("Нажмите Enter для продолжения...");
- Console.ReadLine();
- }
- else
- {
- ChangeColor.redForeground();
- Console.WriteLine("{0} не существует. Измените путь на подобающий. Пример: D:\\photos",mainPath);
- Console.WriteLine("Или в {0} меньше 2 изображений.\n Пожалуйста, выполните сперва команду download", mainPath);
- ChangeColor.ResetColor();
- }
- break;
- }
- if (canDelete == true)
- {
- ChangeColor.redForeground();
- Console.WriteLine("Начинается процесс удаления лишних изображений в главной папке...");
- foreach (var deleteFile in filesToDelete)
- {
- File.Delete(deleteFile);
- Console.WriteLine("{0} удален", deleteFile.ToString());
- }
- ChangeColor.ResetColor();
- canDelete = false;
- }
- }
Решение задачи: «Файл занят другим процессом»
textual
Листинг программы
- new Bitmap(item.ToString());
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д