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:
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 ");
    int indextoremove = int.Parse(Console.ReadLine());
    Console.WriteLine("Do you really want to delete? <Y/N> ");
    string secondinput = Console.ReadLine();
    if (secondinput == "y")
    {

        foreach (string file in jpg)
        {

            if (indextoremove == count && File.Exists(file))
            {
                string pathToFileToDelete = jpg[indextoremove];
                File.Delete(pathToFileToDelete);
                Console.WriteLine("{0} has been deleted successfully", pathToFileToDelete);
            }

        }


What I have tried:

if (indextoremove == count && File.Exists(file))
{
string pathToFileToDelete = jpg[indextoremove];
File.Delete(pathToFileToDelete);
Console.WriteLine("{0} has been deleted successfully", pathToFileToDelete);
}
Posted
Updated 6-Feb-22 22:53pm

C#
string pathToFileToDelete = jpg[indextoremove];

You are using indextoremove as the index into the array of file names. But the value entered by the user is the file number, so it should be indextoremove - 1. But you also have the problem with the line:
C#
if (indextoremove == count && File.Exists(file))

At this point count will be one greater than the number of files in the array, so the two values will never be equal. All you need at this point is to check that indextoremove is greater than zero, and less than count.

And finally, using those two numbers to decide which file to delete makes no sense. You know which file the user wants to delete by the number previously entered, so you just need:
C#
if (secondinput == "y")
{
    string pathToFileToDelete = jpg[indextoremove - 1];
    File.Delete(pathToFileToDelete);
    Console.WriteLine("{0} has been deleted successfully", pathToFileToDelete);
}
 
Share this answer
 
Comments
Ali Al Omairi(Abu AlHassan) 7-Feb-22 5:28am    
hi;
your answer is great. I think you hit the point, but I think the if statement is good and should be edited to
if (indextoremove <= jpg.Length && File.Exists(jpg[indextoremove - 1])) for jpg and
if (indextoremove - jpg.Length <= png.Length && File.Exists(png[indextoremove - jpg.Length - 1])) for png
Richard MacCutchan 7-Feb-22 6:01am    
Thanks you yes, although there are actually two arrays, one for jpg and one for png. No doubt the OP can add the extra tests.
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Sorry, but we can't do any of that for you - we have no access to your file system, so we can't tell what is going on.
 
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