0

What i want to do here was getting an string input from the user and if that string input is in the array i want to delete it from the file (all the items in the array is actual files in my computer that got scanned at the start of the program and become one array) is there a way to do that without foreach?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
using System.Threading;

string typed = null;
            string loc = AppDomain.CurrentDomain.BaseDirectory;
            if (!Directory.Exists(loc + @"\shortcuts"))
            {
                Directory.CreateDirectory(loc + @"\shortcuts");
            }
            string[] directory = Directory.GetFiles(loc + @"\shortcuts");

            foreach (var filed in directory)
            {
                File.Move(filed, filed.ToLowerInvariant());
            }

            string[] file = Directory.GetFiles(loc + @"\shortcuts").Select(System.IO.Path.GetFileNameWithoutExtension).ToArray();

            foreach (string dir in directory)
            {
            }
            if (typed == "exit") System.Environment.Exit(0);

            //other ifs here

            else if (typed == "rem")
                        {
                            //Console.WriteLine("\nNot available at the moment\n");

                            ////add this command
                            Console.WriteLine("\nWhich program entry do you wish to erase?\n");
                            typed = Console.ReadLine().ToLower();
                            if (file.Any(typed.Contains))
                            {
                                File.Delete(file.Contains(typed)); //this is the broken part and i don't know how i can get the stings from there

                                Console.WriteLine("hi");
                            }
                            else Console.WriteLine("\n" + typed + " is not in your registered programs list.\n");

                        }

Expected result was getting rid of the typed program in the folder and actual results was just an error code.

aGoodFellow
  • 41
  • 10

1 Answers1

1

You are storing only the file name in the array, not its complete path or extension. You need to change this, and allow it to store FileName with extension.

string[] file = Directory.GetFiles(loc + @"\shortcuts").Select(System.IO.Path.GetFileName).ToArray();

and then, you need to change the If condition as follows.

if (file.Contains(typed))
{
      File.Delete(Path.Combine(loc + @"\shortcuts",typed));
      Console.WriteLine("hi");          
}

In this Scenario, user would need to input the file name with extension.

If you want the User to input only the filename(without extension, as in your code), then, you could run into a situation where there could be two files with different extension.

"test.jpg"
"test.bmp"

Update

Based on your comment that you cannot store extensions, please find the updated code below. In this scenario, you do not need to change the array. Since you are only storing lnk files, you can append the extension to the file name to complete the path during Path.Combine.

if (file.Contains(typed))
{
      File.Delete(Path.Combine(loc , @"shortcuts",$"{typed}.lnk"));
      Console.WriteLine("hi");          
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • I cannot make it with extension, the program makes the file names look like they are commands themselves, its not a big problem to have two files with different extensions since the program only made for running shortcuts. Also changing that string[] would mess up alot of other things in the program because of how singlesidedly i coded it. But thank you regardless. – aGoodFellow Dec 23 '18 at 01:40
  • 1
    @aGoodFellow I have updated the code based on your comment. Kindly check – Anu Viswan Dec 23 '18 at 01:46
  • Hmm looking at it.. since i only made it for shortcuts i can add "+.lnk" but its always best to have it for all the filetypes if the user messes up something. That also creates a question about how can i prevent user from doing what you just described edit: oh sorry @Anu Viswan i didn't see that for a second there thank you. – aGoodFellow Dec 23 '18 at 01:51
  • 1
    @aGoodFellow Glad to help, kindly mark it as Answer/Helpful if it helped you resolve the issue faced. – Anu Viswan Dec 23 '18 at 03:19
  • say i added a string called "extension" that gets an separate input and then i did this command `File.Delete(Path.Combine(loc, @"\shortcuts", $"{typed}.", extension));` but its not working, any idea? (also i can't do `typed = typed + "." + extension` because of how i did the code so please don't suggest that) – aGoodFellow Dec 23 '18 at 14:40
  • you need to use $"{typed}.{extension}". Complete call, File.Delete(Path.Combine(loc, @"\shortcuts", $"{typed}.{extension}")); – Anu Viswan Dec 23 '18 at 15:09
  • hmm still doesn't work, should i send you all the code? – aGoodFellow Dec 23 '18 at 21:03
  • That should have worked, how is user inputting extension,. Is "." included in the input? Could you post sample inputs of file and extension? – Anu Viswan Dec 23 '18 at 22:46
  • i have this command `extension = extension.Replace(".", string.Empty);` in case of user types ".xxx" instead of "xxx" then i add it in the "." with the `File.Delete(Path.Combine(loc, @"\shortcuts", $"{typed}.{extension}"));` sample inputs are for file its for example "doom" (however loc is current directory) and for extension its like "bat" – aGoodFellow Dec 24 '18 at 01:23
  • 1
    This should be @"\shortcuts" -> "shortcuts". You dont need slash there. Path.Combine would add it. – Anu Viswan Dec 24 '18 at 02:03