How to use DirectoryInfo.EnumerateFiles - c#

Based on the documentation on MSDN for DirectoryInfo.EnumerateFiles, the process involves:
declaring DirectoryInfo with the path
using the EnumerateFiles method to retrieve files
There are underlying classes that retrieve this information such as FileInfo dependent on FileSystemInfo.
DirectoryInfo DR = new DirectoryInfo(#"C:\temp\Downloads");
foreach(FileInfo FF in DR.EnumerateFiles())
{
textBox1.Text += FF.ToString()+ "\r\n";
}
How do I re-code this where I can declare FileInfo before using it
directly?
How would I re-code this if I were to use List<T>?
How would I read the list enumerable data?
Thanks

Why do you want to?
var list = DR.EnumerateFiles().ToList();

It's not all that clear what you are trying to do, but nonetheless, you can 'declare it before using it', by doing the below:
DirectoryInfo DR = new DirectoryInfo(#"C:\temp\Downloads");
List<string> filePaths = DR.EnumerateFiles();
IEnumerable<FileInfo> fileInfos = filePaths.Select(f => new FileInfo(f));
foreach(FileInfo fileInfo in fileInfos)
{
textBox1.Text += fileInfo.FullName.ToString()+ "\r\n";
}
(That is, just move the declaration outside the foreach loop)
Since EnumerateFiles returns the full path of the file, so you'd need to create FileInfo instances out of that, which you can do using the constructor provided for you.
To do the 'list' bit you've talked about, you just add ToList() and LINQ will do the work for you:
List<FileInfo> fileInfos = filePaths.Select(f => new FileInfo(f)).ToList();

Related

Adding two fileinfo[] together

FileInfo[] comList = new FileInfo[]{};
FileInfo[] files
DirectoryInfo dInfo;
string[] folderList = path.Split(',');
foreach (string folder in folderList){
dInfo = new DirectoryInfo(folder);
files = dInfo.GetFiles().Where(F => extensions.Contains(f.Extension.ToLower())).ToArray();
comList.Concat(files);
}
I am trying to read multiple folders and get all the files into one FileInfo[], but after doing the concat to comList, my comList is still empty.
the path input is something like string path = "pathA,pathB,pathC"
if this is not the way to do it, what is a better ways to get all the files from different directory into one.
According to Microsoft documentation Concat will returns a new collection without modifying existing one:
public static System.Collections.Generic.IEnumerable<TSource> Concat<TSource> (
this System.Collections.Generic.IEnumerable<TSource> first,
System.Collections.Generic.IEnumerable<TSource> second);
Therefore in your scenario assigning the statement back to comList as following and you're good to go!
comList = comList.Concat(files);
Nonetheless, if you're not constraining yourself to use array for comList, you may consider to use List instead which allow us to achieve the same without redundant cast:
List<FileInfo> comList = new List<FileInfo>();
...
foreach (string folder in folderList) {
var dInfo = new DirectoryInfo(folder);
var files = dInfo.GetFiles().Where(f =>
extensions.Contains(
f.Extension.ToLower()));
comList.AddRange(files);
}
You need to use the return of comList.Concat(files), like:
comList = comList.Concat(files).ToArray();
The ToArray() method is needed because Concat() returns an IEnumerable.
Alternatively you can make comList an actual List<FileInfo> and use its AddRange method in each iteration:
comList.AddRange(files);

Get the array file names order by name files c#

DirectoryInfo d = new DirectoryInfo(mypath);//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.jpg"); //Getting Text files
How the files array could be order by name?
let's say
files[0].Name is 'hi1.jpg'
files[1].Name is 'hi2.jpg'
and so on
It is just a call to OrderBy in Linq namespace
using System.Linq;
....
FileInfo[] Files = d.GetFiles("*.jpg").OrderBy(x => x.Name).ToArray();
By the way, I suggest you to use EnumerateFiles instead of GetFiles. In particular if you have to loop over the result like this
foreach(FileInfo fi in d.EnumerateFiles("*.jpg").OrderBy(x => x.Name))
Console.WriteLine(fi.Name);
As explained in the MSDN documentation
The EnumerateFiles and GetFiles methods differ as follows: When you
use EnumerateFiles, you can start enumerating the collection of
FileInfo objects before the whole collection is returned. When you
use GetFiles, you must wait for the whole array of FileInfo objects to
be returned before you can access the array.
FileInfo[] Files = d.GetFiles("*.jpg").OrderBy(f => f.Name).ToArray();
Using Linq
d.GetFiles("*.jpg").OrderBy(file=> file.Name).ToArray();
FileInfo[] files = d.GetFiles("*.jpg").OrderBy(file => file.Name).ToArray();
As answered above .ToArray() function along with OrderBy(columnName) does the job.

How to convert openFileDialog.FileNames to FileInfo[]

I have a OpenFileDialog and I want to convert selected file names to a FileInfo[] variable.
But I don't know how to convert all the selected files in one line code.
This obviously doesn't work:
FileInfo[] files = openFileDialog.FileNames;
Thank you.
Using LINQ:
FileInfo[] files = openFileDialog.FileNames.Select(f => new FileInfo(f)).ToArray();
The FileInfo class offers a constructor that expects a filename. Therefore, to get a FileInfo instance for a single filename string, simply call that constructor:
FileInfo file = new FileInfo(openFileDialog.FileName);
In your case, you want to get an array and have several filename strings, therefore you can use the LINQ extension methods from the Enumerable class:
FileInfo[] files = openFileDialog.FileNames.Select(fn => new FileInfo(fn)).ToArray();
Note the additional call to ToArray in the end, as Select will return an IEnumerable<FileInfo>.

Iterate through files on a certain level

I'm trying to iterate through all the files on a certain level in the folder hierachy, more specifically, in all the sub-sub folders. Before I do actual operations on the files, I also want to count all the files to be able to show a progress bar. This means the iterating method must be called 2 times. This is the relevant code, I'm using now:
Iterate(bool count)
{
foreach (string dir in Directory.GetDirectories(root))
foreach (string subdir in Directory.GetDirectories(dir))
foreach (string file in Directory.GetFiles(subdir))
{
if (count) progressBar.Maximum++;
else
{
//do operations
}
}
}
I'm wondering if there's a better way of doing this. Surely there must be a better way than adding a foreach for every folder level..?
It'd be easier to me to use LINQ here.
var files =
(from dir in Directory.GetDirectories(root)
from subdir in Directory.GetDirectories(dir)
from f in Directory.GetFiles(subdir)
select f).ToList();
var fileCount = files.Length;
foreach (var f in files) {
...
}
Before your GetFiles foreach, try this:
string [] fileEntries = Directory.GetFiles(subdir);
int intFileCount = fileEntries.length;
Or it can replace it, if the loop only serves to count the files.
The documentation of Directory.GetFiles shows how to recursively iterate a directory tree
you could download the fluent thing I wrote for System.IO (see here: http://blog.staticvoid.co.nz/2011/11/staticvoid-io-extentions-nuget.html) and then use this LINQ statement
var files = from d in di.Directories()
from dir in d.Directories()
from f in dir.Files()
select f;
write this instead your code
string [] files = Directory.GetFile("yourDirectory","*.*",SearchOptions.AllDirectories);
this will return all files in sub-directories instead of using recursion

Trimming all strings from an array in c#

I used this code to get the contents of a directory:
string[] savefile = Directory.GetFiles(mcsdir, "*.bin");
comboBox1.Items.AddRange(savefile);
and it returns as
C:\Users\Henry\MCS\save1.bin
C:\Users\Henry\MCS\save2.bin
How can I make it return as only
save1.bin
save2.bin
Please note that this app will be used by other people, so the name is not always "Henry".
Thank you.
I would recommend using DirectoryInfo.GetFiles instead, and LINQ:
FileInfo[] savefile = new DirectoryInfo(mcsdir).GetFiles("*.bin");
comboBox1.Items.AddRange(savefile.Select(x => x.Name).ToArray());
Use LINQ:
var strs = savefile.Select(a => Path.GetFileName(a)).ToArray();
Looking at the suggestion of minitech:
As long as you get the array of type FileInfo[] there is no need to convert it to string array. Just set the property DisplayMember to the property name you want to display in your ComboBox.
FileInfo[] savefile = new DirectoryInfo(mcsdir).GetFiles("*.bin");
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = savefile;
Using this you keep your original FileInfo[] array with all additional information (as to the full path to your files) and same time display only the short filenames (without path) in your control.
(I assume that your question is about WinForms. If you are using Silverlight or WPF you need to set the property using the "Target" attribute).
Use Path.GetFileName(string path).

Categories

Resources