How do i get the last saved image on hard disk? [duplicate] - c#

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.

Related

How do I do a filePath wich depens on the location of the .exe [duplicate]

This question already has answers here:
get path for my .exe [duplicate]
(4 answers)
Closed 1 year ago.
I am doing a program which reads .txt files and I want to access those without having to change manually the filePath directory. I have it like this: "F:\TR\AppPathFinding\AppPathFinding\bin\Debug" Here I have the .exe file, which starts the program. Secondly, I want to access one folder with different .txt files. It is here: "F:\TR\AppPathFinding\AppPathFinding\bin\Debug\GrillSelection" and the .txt name is: "SetGrill1.txt".
How can I access this .txt without having to change the path manually?
filePath = #"F:\TR\AppPathFinding\AppPathFinding\bin\Debug\GrillSelection\SetGrill1.txt";
This is what I need to change.
Someone can help me please?
As far as I understand you always want to access the same folder that is located at your .exe. Using a relative path.
This is very well explained by Kieren Johnstone.
string filePath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + #"\GrillSelection\SetGrill1.txt";

Search folder and get files [duplicate]

This question already has answers here:
C# Searching for files and folders except in certain folders
(4 answers)
Closed 8 years ago.
I am having one folder containing all .jpg files. Now I am having some part of file name. I want all files whose name contains that part from that folder.
thank you in advance...
You can use DirectoryInfo.EnumerateFiles to do this. And just give it the search pattern.

How to sort DirectoryInfo.GetFiles() [duplicate]

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))
{
}

Find files not matching a pattern [duplicate]

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();

Is FileInfo.Exists/Copy faster than File? [duplicate]

This question already has answers here:
What is the difference between File and FileInfo in C#?
(10 answers)
Closed 9 years ago.
As the title says is which one of the scenarios below is faster?
// Using FileInfo
FileInfo file = new FileInfo(#"C:\Test.txt");
if (file.Exists)
file.CopyTo(#"C:\TestCopy.txt");
// Using File
if (File.Exists(#"C:\Test.txt"))
File.Copy(#"C:\Test.txt", #"C:\TestCopy.txt");
I know the FileInfo is easier for the eye to read, but is one method faster than the other?
Difference is that FileInfo cache information: file existing check is executed once.
Then, if you check Exists property and THEN create file, new call to Exists property always will return false.

Categories