Solved

Convert filename to FolderItem Object?

Posted on 2011-03-04
3
719 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where did System.Data.Objects go? 2 28
Search an image for an image 3 30
HTML - Color not displaying correctly in EMAIL. 6 40
How come this regular expression allows "e"? 14 38
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

810 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