Convert filename to FolderItem Object?

Is there a way to convert a filename (string) to a FolderItem Object?
Who is Participating?
Todd GerbertIT ConsultantCommented:
This example uses LINQ extension methods (which requires .Net 3.5) and the Enum.HasFlag method (which requies .Net 4.0).

The basic steps are get a DirectoryInfo object for the folder in question.  Call the EnumerateFiles() method to get a list of all FileInfo objects for the files in that folder and below.  Loop through the FileInfo's excluding items with Hidden or System attributes.

This mushes everything together in one line.
string[] fileList = new DirectoryInfo("C:\\some\\folder").EnumerateFiles("*", SearchOption.AllDirectories).Where(x => !x.Attributes.HasFlag(FileAttributes.Hidden) && !x.Attributes.HasFlag(FileAttributes.System)).Select(y => y.FullName).ToArray();

Open in new window

This is basically the same thing, just written out with a longer syntax (and compatible with .Net 2.0):
static string[] ListFiles(string folderPath)
	List<string> fileList = new List<string>();
	DirectoryInfo folder = new DirectoryInfo(folderPath);
	foreach (FileInfo file in folder.EnumerateFiles("*", SearchOption.AllDirectories))
			if ((file.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden &&
			(file.Attributes & FileAttributes.System) != FileAttributes.System)
		catch { }
	return fileList.ToArray();

Open in new window

I believe you will need to manually enum each folder individually in order to catch exceptions when a folder is encountered to which you don't have access.
Here you go:

The green reply is the answer and it works just fine. You don't need to use an array. Just put your string in for the array(i) value.
deleydAuthor Commented:
I couldn't quite follow. I should be more specific. To get a list of all files in a directory tree, including files in subdirectories, I can do:
            string[] files = Directory.GetFiles(@"C:\Users\Public\My Music",

Open in new window

which is nice and easy and fast, one line of code. (Except it also gives me hidden system files, which I really don't want. Is there a way to say don't include hidden system files?)

But to get a FolderItem Object for a file I have to do:
            DirectoryInfo source = new DirectoryInfo(@"C:\Users\Public\My Music");
            Shell32.Folder folder = shell.NameSpace(source.FullName);
            foreach (FileInfo fi in source.GetFiles())
                Shell32.FolderItem fileItem = folder.ParseName(fi.Name);
                value = folder.GetDetailsOf(fileItem, Shell32File_TrackName_idx);

Open in new window

which is a lot of work, plus this only gives me the files in one directory, I have to manually traverse the directory tree structure to get all files.

So I wondered if there was a quick short cut way to get a fileItem from the file name I can easily get in the first example?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.