Solved

Convert filename to FolderItem Object?

Posted on 2011-03-04
3
747 Views
Last Modified: 2012-05-11
Is there a way to convert a filename (string) to a FolderItem Object?
0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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