I'm currently working on a .NET 2.0 application and I got stuck while trying to loop through all files in a specific folder and determine its file pairs. (Files with the same name but different extensions)
Like:
I'm interested in the filename extension, if they are available or not, that's why I created a class called "FileClass". At the end the file pairs will be added to a list list.AddRange()
.
Her is my code example:
class FileClass
{
public string FileName { get; set; }
public string filePath { get; set; }
public bool mp3 { get; set; }
public bool txt { get; set; }
public bool jpg { get; set; }
}
static void Main()
{
var list = new List<FileClass>();
var dirConfig = new DirectoryInfo(@"New Folder");
var allFiles = dirConfig.GetFiles("*");
foreach (var fileInfo in allFiles)
{
// Some code for finding file pairs...
// set properties, if filename extension is available
// and add them to a list.
}
}
Any suggestion how to handle this?
if possible without LINQ
I would use a Dictionary<String, List<String>>
(for filename -> list of extensions)
The first step could look like this:
Dictionary<String, List<String>> d = new Dictionary<string, List<string>>();
var dirConfig = new DirectoryInfo(@"SomeFolder");
var allFiles = dirConfig.GetFiles("*");
foreach (var fileInfo in allFiles)
{
String coreName = Path.GetFileNameWithoutExtension(fileInfo.Name);
if (!d.ContainsKey(coreName)) d.Add(coreName, new List<String>());
d[coreName].Add(fileInfo.Extension);
}