Suggestion: write it in pseudo code and then translate into C#.
E.g.
for each file name in directory do loop
prefix = file name up to and excluding first "_"
write prefix if prefix is not contained in Table
end loop
Translating into C# is left as exercise.
Cheers
Andi