-1

When I run the program and input "1 20", my program will delete 0 1 2 20. What I want it to do was just to delete 1 and 20 only.

The "list of files".

The portion of my program that I'm having issues with:

Console.WriteLine("Do you want to delete file? (Y/N)");
string answer = Console.ReadLine();
string dir = @"C:\Users\1\Downloads\Project Resources Files\";
string[] textFiles1 = Directory.GetFiles(dir, "*", SearchOption.AllDirectories);
int counter = 0;
         
if (answer == "y" || answer == "Y")
{
    Console.WriteLine("Select file to delete. To delete file, type file No<space>file No. Example: 1 3");
    string keyin = Console.ReadLine();                    
    Console.WriteLine("Do you really want to delete? (y/n)");
    string cfm = Console.ReadLine();
    if(cfm == "Y" || cfm == "y")
        foreach (var file in textFiles1)
        {
            if (keyin.Contains(counter.ToString()))
            {
                File.Delete(file);
                Console.WriteLine("\n File number " + counter + " deleted sucessfully");
            }
            counter++;
        }
    if(cfm == "n" || cfm == "N")
    {
        goto back;
    }
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
R3ASON
  • 19
  • 3
  • Please dont post code as an image. – Connor Stoop Feb 27 '23 at 14:23
  • 1
    Welcome to Stack Overflow! Relevant code and error messages need to be included in your question *as text*, [not as pictures of text](https://meta.stackoverflow.com/q/285551/328193). Just linking to screen shots makes it more difficult for people to help you. To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Feb 27 '23 at 14:24
  • Additionally... This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Feb 27 '23 at 14:24
  • Sorry, I'm new to programming and just found out about this site. Apologies, I have adjusted the question. – R3ASON Feb 27 '23 at 14:31
  • You can show respect for those who might help you by making the basic effort to ensure your code samples are well-formatted, rather than indented way to the right as was initially the case, and by avoiding posting images or screen shots of technical content like sample data. – Joel Coehoorn Feb 27 '23 at 15:02

2 Answers2

2

Firstly, I would recommend you to split the input string to separate numbers. You can do it by using System.Linq:

int[] splitNums = inputString.Split(' ').Select(int.Parse).ToArray();

After that you will be able to check if splitNums contains your counter value and handle the needed scenario:

if (splitNums.Contains(counter))
{
    // delete your file
}

Here is the simple example: https://dotnetfiddle.net/2G9QAo

Ivan Vydrin
  • 273
  • 7
0

The problem is this line:

if (keyin.Contains(counter.ToString()))

The keyin a simple string variable with the value of 1 20. It's not an array or a set of numbers: it's one string. We also see that counter is an incrementing integer, but is converted a string for each comparison.

This means we're doing simple string .Contains() checks, with no regard for any numeric meaning of the original inputs. Therefore the 1 20 string value does indeed contain a 0 string. It's right there as the last character. It also contains a 1 (first character), a 2 (3rd character), and a 20 (last two).

To fix this, you probably want to do more work on the keyin variable so you a have a set of integers, and then change the comparison to check the set. There are many ways to do this, including one other answer, but you're probably best served in this case to make your own attempt first.


Finally, the initial if(cfm) check is missing the curly braces, which might be why you felt the need for an ugly goto line. Add in the correct bracing and the second conditional if(cfm) check can probably be removed.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794