How to convert FileInfo into FileInfo[] - c#

I have been working on a program that requires a different approach to finish a job using try and catch nested within another try/catch.
For this purpose I have had to create a set of files as strings and then converted them to FileInfo.
IEnumerable<string> paths = null;
foreach (String fil in paths)
FileInfo h = new FileInfo(fil);
So That wasn't so difficult, I require FileInfo to be in the form of a FileInfo[] array to continue the program however.
System.IO.FileInfo[] files = null;
Simply put, what is the best method to convert one type to the other, either directly from the string of from the converted FileInfo into a FileInfo array (FileInfo[])?

Yeah or create a single-item array:
FileInfo[] files = new FileInfo[] { info };

Why not directly?
paths.Select(p => new FileInfo(p)).ToArray();

Use:
var result = paths.Select(p => new FileInfo(p)).ToArray();

You could just use Linq select to create your FileInfo[]
IEnumerable<string> paths = null; // assuming you are going to fill this with filenames
FileInfo[] fileInfos = paths.Select(p => new FileInfo(p)).ToArray();

Related

Get file extensions to populate into a ComboBox in C#

So I have a folder with some imagens in many extensions like .ico, .png, .jpg, etc. and I've populated it into a comboBox using this code:
string caminho = #"C:\Users\User1\Desktop\Test\";
DirectoryInfo dir = new DirectoryInfo(caminho);
FileInfo[] fi = dir.GetFiles();
foreach (var ficheiro in fi)
{
string caminhoF = caminho + ficheiro.ToString();
string extension = Path.GetExtension(caminhoF);
comboBox1.Items.Add(extension);
}
The code is getting all the existing extensions in this path and put it on the comboBox, but it displays like this:
.ico
.ico
.ico
.png
.png
.jpg
.jpg
and I want to simply display each one of the existing extensions like grouping them.
Could you help me with that?
You can get the file extension from the FileInfo. You can also use Linq Distinct() to get unique extensions.
string caminho = #"C:\Users\User1\Desktop\Test\";
DirectoryInfo dir = new DirectoryInfo(caminho);
var extensions = dir.GetFiles().Select(fi => fi.Extension).Distinct();
foreach (var extension in extensions) {
comboBox1.Items.Add(extension);
}
Ok, I was to find a solution for it. Here it is the code:
string caminho = #"C:\Users\User1\Desktop\Test\";
DirectoryInfo dir = new DirectoryInfo(caminho);
FileInfo[] fi = dir.GetFiles();
foreach (var ficheiro in fi)
{
string caminhoF = caminho + ficheiro.ToString();
string extension = Path.GetExtension(caminhoF);
if (!comboBox1.Items.Contains(extension))
{
comboBox1.Items.Add(extension);
}
}
LINQ-to-Objects makes this easy. LINQ is similar to SQL but allows chaining transformations.
var comboBox1 = new ComboBox();
var caminho = #"C:\Users\User1\Desktop\Test\";
var dir = new DirectoryInfo(caminho);
var extensions = dir.GetFiles()
.Select(fi => fi.Extension)
.OrderBy(ext => ext, StringComparer.CurrentCulture)
.Distinct(StringComparer.CurrentCultureIgnoreCase)
.ToArray();
comboBox1.Items.AddRange(extensions);
Here are the rough steps:
Scan your folder to find out what files it contains.
Extract the file extension from each file you find.
Using a data structure that stores only unique entries, add extensions that you find to be new to the structure.
Iterate over the data structure to populate your combobox.
The part that you need is to find a data structure that helps you store unique values.
HashSet<T> has your back here: it allows quick lookups to determine set membership ("does the set already contain some element x?").
string caminho = #"C:\Users\User1\Desktop\Test\";
DirectoryInfo dir = new DirectoryInfo(caminho);
FileInfo[] fi = dir.GetFiles();
HashSet<string> extensions = new HashSet<string>;
foreach (var ficheiro in fi)
{
string caminhoF = caminho + ficheiro.ToString();
string extension = Path.GetExtension(caminhoF);
// If the set does not contain this extension, it'll be added and
// `Add()` will return true. Otherwise, it will do nothing and `Add()`
// will return false.
extensions.Add( extension );
}
foreach( var extension in extensions ) {
comboBox1.Items.Add(extension);
}

C# File Last Modified time

I am trying to get the last write time of a particular file. This is the code and it works:`
DirectoryInfo DR = new DirectoryInfo(folderPath);
FileInfo[] FR2 = DR.GetFiles("InputData.csv");
var FileLastModified= null;
foreach (FileInfo F1 in FR2)
{
FileLastModified = F1.LastWriteTime;
}
FileLastModified gives me the last write time and I am only interested to find the time of this InputData.csv file. The problem is I do not want to use a for loop and need the write time for just one particular file. Is there a better way to write this without the loop?
You don't have to search through a directory to get a FileInfo - you can construct one directly from the full path. It sounds like you just need:
var fileInfo = new FileInfo(Path.Combine(folderPath, "InputData.csv"));
var lastModified = fileInfo.LastWriteTime;
Yes, you can just pass the path to the file you're interested in to a new FileInfo object.
var fileInfo = new FileInfo(pathToFile);
var fileLastModified = fileInfo.LastWriteTime;

Get file name with system IO

I'm working on a C# script that has to access a random file during runtime, the problem is that the files are being generated on the fly by another source and I have no means of knowing their names, I have solved a first issue which is to get how many files there are in my working directory:
s = #"C:\Imagenes";
System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(s);
int files;
files = d.GetFiles().Length;
Debug.Log(files.ToString());
return files;
Now I would like to acces a random element in my working dicrectory, but since I don't have a clue what their names are, is there a way to get their names by index or something?
DirectoryInfo.GetFiles will give you array of fileInfo objects. From that you can get the file name using FileInfo.Name
You need to use the FileInfo objects that are returned by d.GetFiles():
DirectoryInfo d = new DirectoryInfo("c:\\path");
foreach (FileInfo file in d.GetFiles())
{
string name = file.Name;
}
try
FileInfo[] fileinfos = d.GetFiles();
foreach (FileInfo FI in fileinfos)
{
string fullname = FI.FullName;
string name = FI.Name;
// do someting...
}
see
http://msdn.microsoft.com/en-us/library/4cyf24ss.aspx
http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx
Not sure why you want a random file, but this should work (except files get deleted during calculation of length and getting a rondom one)
int length = d.GetFiles().Length;
Random rnd = new Random();
var randomFile = d.GetFiles().ElementAt(rnd.Next(0, length-1);

Sorting the result of Directory.GetFiles in C#

I have this code to list all the files in a directory.
class GetTypesProfiler
{
static List<Data> Test()
{
List<Data> dataList = new List<Data>();
string folder = #"DIRECTORY";
Console.Write("------------------------------------------\n");
var files = Directory.GetFiles(folder, "*.dll");
Stopwatch sw;
foreach (var file in files)
{
string fileName = Path.GetFileName(file);
var fileinfo = new FileInfo(file);
long fileSize = fileinfo.Length;
Console.WriteLine("{0}/{1}", fileName, fileSize);
}
return dataList;
}
static void Main()
{
...
}
}
I need to print out the file info based on file size or alphabetical order. How can I sort the result from Directory.GetFiles()?
Very easy with LINQ.
To sort by name,
var sorted = Directory.GetFiles(".").OrderBy(f => f);
To sort by size,
var sorted = Directory.GetFiles(".").OrderBy(f => new FileInfo(f).Length);
To order by date: (returns an enumerable of FileInfo):
Directory.GetFiles(folder, "*.dll").Select(fn => new FileInfo(fn)).
OrderBy(f => f.Length);
or, to order by name:
Directory.GetFiles(folder, "*.dll").Select(fn => new FileInfo(fn)).
OrderBy(f => f.Name);
Making FileInfo instances isn't necessary for ordering by file name, but if you want to apply different sorting methods on the fly it's better to have your array of FileInfo objects in place and then just OrderBy them by Length or Name property, hence this implementation. Also, it looks like you are going to create FileInfo anyway, so it's better to have a collection of FileInfo objects either case.
Sorry I didn't get it right the first time, should've read the question and the docs more carefully.
You can use LINQ if you like, on a FileInfo object:
var orderedFiles = Directory.GetFiles("").Select(f=> new FileInfo(f)).OrderBy(f=> f.CreationTime)
try this, it works for me
[System.Runtime.InteropServices.DllImport("Shlwapi.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
private static extern int StrCmpLogicalW(string psz1, string psz2);
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] arrFi = di.GetFiles("*.*");
Array.Sort(arrFi, delegate(FileInfo x, FileInfo y) { return StrCmpLogicalW(x.Name, y.Name); });
This works if you only need to sort by file name and the file title supports the ascending or descending logical order.I add variables to have a bit more control of the source and search pattern.
Using Dir = System.IO;
string Source = yourVariable;
string SearchPattern = yourVariable;
Dir.Directory.GetFiles(Source, SearchPattern, Dir.SearchOption.AllDirectories).OrderBy(s => s).ToList();
// Getting Directory object
DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
// getting files for this folder
FileInfo[] files = directoryInfo.GetFiles();
// Sorting using the generic Array.Sort function based on Names comparison
Array.Sort(files, delegate (FileInfo x, FileInfo y) { return String.Compare(x.Name, y.Name); });
// Sorting using the generic Array.Sort function based on the creation date of every folder
Array.Sort(files, delegate (FileInfo x, FileInfo y) { return DateTime.Compare(x.CreationTime, y.CreationTime); });
Array Sort

C# list file in directory issue

I am using VSTS 2008 + C# + .Net 3.0. I want to enumerate all files in a directory by creation time, i.e. files created more recently will be enumarate at first, older files will be enumerated at last. Any ideas how to implment this?
Something like that
System.IO.FileInfo[] array = new System.IO.DirectoryInfo("directory_path").GetFiles();
Array.Sort(array, delegate(System.IO.FileInfo f1, System.IO.FileInfo f2)
{
return f2.CreationTimeUtc.CompareTo(f1.CreationTimeUtc);
});
I would probably use LINQ and a list... something like this should work:
DirectoryInfo di = new DirectoryInfo("YourPath");
List<FileInfo> files = di.GetFiles().OrderBy(f => f.CreationTime).ToList();
foreach (FileInfo file in files)
{
//do something
}
Try somithing like this:
DirectoryInfo di = new DirectoryInfo("path to folder");
FileInfo[] files = di.GetFiles();
IOrderedEnumerable<FileInfo> enumerable = files.OrderBy(f => f.CreationTime);
foreach (FileInfo info in enumerable)
{
// do stuff...
}
EDIT: updated, here's a non-LINQ solution
FileInfo[] files = new DirectoryInfo("directory").GetFiles();
Array.Sort(files, delegate(FileInfo f1, FileInfo f2) {
return f2.CreationTime.CompareTo(f1.CreationTime);
});
The above will sort by latest to oldest. To sort by oldest to latest change the delegate to: return f1.CreationTime.CompareTo(f2.CreationTime);
LINQ solution:
FileInfo[] files = new DirectoryInfo("directory").GetFiles();
var results = files.OrderByDescending(file => file.CreationTime);
Use OrderByDescending to sort by most recent CreationTime, otherwise use OrderBy to sort from oldest to newest CreationTime.
DirectoryInfo baseFolder=new DirectoryInfo("folderName");
FileInfo[] files=baseFolder.GetFiles("");
for(int i=1; i<=files.Length;i++)
for(int j=1; j<files.Length;j++)
{
if(files[j].CreationTime > files[j+1].CreationTime)
{
FileInfo f = files[j];
files[j] = files[j+1];
files[j+1] = f;
}
}

Categories