Search code examples
c#arraysshortcutlnk

c# is there a way to get more than one files name without their path and their extension in an array?


For short what i want to do is get the names of the files in a certain folder as an array kind of like the example at the bottom, however it gets the full path instead of just name of the file, also keeps the .lnk part which i don't want to.

string[] directory = Directory.GetFiles(@"C:\Program Files (x86)\programloc\shortcuts","*.lnk");

I was wondering if i could do it the same way as i did the full path however it's not working in a way i hoped due to snytax.

// set the directory here to "C:\Program Files (x86)\programloc\shortcuts" call it as "string paths;"
//string[] file = paths.GetFiles("*.lnk");??????

foreach (string dir in directory)
        {
         //adding "Console.WriteLine(file);" in here should give me the .lnk files in that folder without their path or extension
        }

Solution

  • You can use a bit on Linq and Path.GetFileNameWithoutExtension

    string[] directory = Directory.GetFiles(@"C:\Program Files (x86)\programloc\shortcuts", "*.lnk")
        .Select(System.IO.Path.GetFileNameWithoutExtension)
        .ToArray();