Solved

Convert filename to FolderItem Object?

Posted on 2011-03-04
3
689 Views
Last Modified: 2012-05-11
Is there a way to convert a filename (string) to a FolderItem Object?
0
Comment
Question by:deleyd
3 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 35038338
Here you go: http://stackoverflow.com/questions/314008/c-programatically-using-a-string-as-object-name-when-instantiating-an-object

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.
0
 

Author Comment

by:deleyd
ID: 35046824
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",
                "*.*",
                SearchOption.AllDirectories);

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?
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35061449
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))
	{
		try
		{
			if ((file.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden &&
			(file.Attributes & FileAttributes.System) != FileAttributes.System)
				fileList.Add(file.FullName);
		}
		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.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now