This question already has answers here:
Sorting Directory.GetFiles()
(13 answers)
Closed 8 years ago.
I am creating the image of the PowerPoint file programmatically. And after saving the Images to the local drive I am getting the files using DirectoryInfo.GetFiles().
I am saving the image files with the sequence numbers.
My Files:
My issue is when I get the files, are not in the sequence I need them in.
The files sequence which I am getting in the FileInfo[] is :
Can any one help me to solve this issue?
The function doesn't make any guarantees about order but you can achieve the desired result with a simple LINQ query;
FileInfo[] sortedFiles = DirectoryInfo.GetFiles().OrderByDescending(x => x.Name).ToArray();
Try this
foreach (FileInfo fi in directory.GetFiles().OrderBy(fi=>fi.FileName))
{
}
Related
This question already has answers here:
Exact file extension match with GetFiles()?
(8 answers)
Closed 4 years ago.
I need to get all .txt files in all subfolders of a specified folder so I do:
var files = Directory.GetFiles(reportsFolder, "*.txt", SearchOption.AllDirectories);
However, in some folders I also have files with extensions like .txt_TODO, and these are also being retrieved by GetFiles.
How can I skip these files?
Just filter the result by comparing the extension to ".txt", e.g. using LINQ:
var files = Directory.GetFiles(reportsFolder, "*.txt", SearchOption.AllDirectories)
.Where(f => Path.GetExtension(f).Equals(".txt", StringComparison.OrdinalIgnoreCase))
.ToArray();
This question already has answers here:
Getting all file names from a folder using C# [duplicate]
(7 answers)
Closed 4 years ago.
I need to search a root folder for particular subdirectory named "XYZ","ABC". And need to get the filenames from these two folder by iterating these two folder one by one.
I have used the below code to find subdirectory but not sure how to find filenames from this list.
IEnumerable<string> list = Directory.GetDirectories(root).Where(s => s.Equals("XYZ"));
The easiest way would be:
List<string> allFiles = new List<string>();
foreach (string subDir in list)
allFiles.AddRange (Directory.GetFiles (subDir));
The result is all files in both directories in one list with full path included.
This question already has answers here:
Visual C#: Move multiple files with the same extensions into another directory
(2 answers)
Closed 5 years ago.
The following code is working, but I need to amend it so it moves all .txt files instead of just the test.txt file.
File.Move(#"C:\Desktop\test\test.txt", #"C:\Desktop\test\old\test.txt");
How can I amend it to do this?
It's moving them from the test folder to the test\old subfolder
whith this you can get all files with path and after that with for you can copy all files to your path
string[] files = System.IO.Directory.GetFiles(path, "*.txt");
This question already has answers here:
How to find the most recent file in a directory using .NET, and without looping?
(11 answers)
Closed 7 years ago.
I have this line:
imageList = Directory.GetFiles(#"e:\webbrowserimages\", "*.bmp").ToList();
This is a List
But i want now just one string variable single variable not a list that will contain the last saved image file on the hard disk.
If for example i have 10 images on the hard disk and the first one is: Image0.bmp then Image1.bmp so the string variable will contain Image10.bmp
And then if on my hard disk there are 24 images then the variable string should contain Image24.bmp
You can do this to get the latest bitmap file in the direcotry,
var directory = new DirectoryInfo(#"e:\webbrowserimages\");
var myFile = (from f in directory.GetFiles("*.bmp")
orderby f.LastWriteTime descending
select f).First();
Unless no other parameter is available better not relying on file name in filtering latest image.
This question already has answers here:
Using Directory.GetFiles with a regex in C#?
(4 answers)
Exclude certain file extensions when getting files from a directory
(9 answers)
Closed 9 years ago.
For finding all .txt files, we can use this:
Directory.GetFiles(#"c:\","*.txt")
Is there any way to find all files not matching a pattern (for ex: all files not having extension .txt).
You can try LINQ:
var files = Directory.EnumerateFiles("C:\\").Where(x => !x.EndsWith(".txt")).ToList();
No builtin way as search pattern. But you could use Linq:
var files = Directory.EnumerateFiles(dir)
.Where(fn => !Path.GetExtension(fn).Equals(".txt", StringComparison.OrdinalIgnoreCase))
.ToArray();
Note that i've used EnumerateFiles instead of GetFiles. The latter loads al files into memory before you can start processing, with EnumerateFiles you can start enumerating and filtering the collection of names before the whole collection is returned.
use linq
var files = Directory.GetFiles(dir)
.Where(file=> !file.EndsWith(".txt").ToList();