Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
static void ViewImageFolder()
       {
           string[] jpg = Directory.GetFiles(@"D:\Image", "*.jpg");
           string[] png = Directory.GetFiles(@"D:\Image", "*.png");
           int count = 1;
           string no = "No";
           string name = "Name and location of file";
           string type = "Type of File";
           string typeoffilejpg = "image/jpeg";
           string typeoffilepng = "image/png";
           Console.WriteLine("{0}{1}{2}", no.ToString().PadRight(50, ' '), name.ToString().PadRight(50, ' '), type.ToString().PadRight(50, ' '));
           foreach (string file in jpg)
           {
               Console.WriteLine("{0}{1}{2}", count++.ToString().PadRight(50, ' '), file.ToString().PadRight(50, ' '), typeoffilejpg.ToString().PadRight(50, ' '));
           }

           foreach (string file in png)
           {
               Console.WriteLine("{0}{1}{2}", count++.ToString().PadRight(50, ' '), file.ToString().PadRight(50, ' '), typeoffilepng.ToString().PadRight(50, ' '));
           }

           Console.WriteLine("Completed Scan!");

           Console.WriteLine("Do you want to delete file? (y/n)");
           string input = Console.ReadLine();
           if (input == "y")
           {
               Console.WriteLine("Select file to delete. To delete file, type file No<space>file No. Example: 1 3 ");

               Console.WriteLine("Do you really want to delete? <Y/N> ");
               string secondinput = Console.ReadLine();
               if (secondinput == "y")
               {

                   foreach (string file in jpg)
                   {
                       if (File.Exists(file))
                       {
                           int numIndex = Array.IndexOf(jpg, indextoremove);
                           jpg = jpg.Where((val, idx) => idx != numIndex).ToArray();
                           File.Delete()
                       }




                   }

                   foreach (string file in png)
                   {

                   }
               }
           }



           else
           {

           }


What I have tried:

if (secondinput == "y")
{

foreach (string file in jpg)
{
if (File.Exists(file))
{
int numIndex = Array.IndexOf(jpg, indextoremove);
jpg = jpg.Where((val, idx) => idx != numIndex).ToArray();
File.Delete()
}
Posted
Updated 5-Feb-22 23:36pm

1 solution

Your jpg array contains full paths to files, so unless the indextoremove contains exactly the same string (which is unlikely given the variable name) this code isn't going to return anything useful:
int numIndex = Array.IndexOf(jpg, indextoremove);
In fact, what it probably returns is always -1.

If indextoremove contains a number then use that directly:
C#
string p[athToFileToDelete = jpg[indextoremove];
and you'll get the right string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900