Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert filename to FolderItem Object?

Posted on 2011-03-04
3
Medium Priority
?
784 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline
Suggested Courses

877 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