Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
I have a folder in my computer and inside folder i have many txt files who created by another programm.
i want to make a textbox that capture the filename of the last txt file that create the programm.
i know that it is dificult for me to do that
Posted
Comments
BillWoodruff 20-Dec-15 11:57am    
So, get started by reading the code examples for reading a Directory which are available, as Richard shows you, on MSDN, here, and on StackOverFlow.

Are you going to need to search Directories within the target Directory ?
Stm21 20-Dec-15 12:07pm    
this folder create a folder per day
BillWoodruff 20-Dec-15 12:21pm    
May I point out that you are not "new" on CodeProject; you've been coming here since March 15, this year. I wonder what's going on that you are still unable to write a clear question, or respond to direct questions: is it a problem with English, or, are you just not studying C# very much ?

A key piece of information here is whether you are only going to get the length of Files in your target Directory, or are you going to need to recursively search each Directory in the target Directory.

When, and if, you post some code showing you are making an effort here, I and other folks will be happy to assist you.

Solution posted by webmaster442:


Based on the documentation and with some use of linq :)
C#
var files = Directory.GetFiles("your_folder_here", "*.txt");
var selector = from i in files
               orderby new FileInfo(i).CreationTime descending
               select i;
var last = selector.FirstOrDefault();
TextBox.Text = last;
 
Share this answer
 
v2
Comments
Stm21 20-Dec-15 13:08pm    
The name 'files' does not exist in the current context
i place the using System.IO;
Richard MacCutchan 21-Dec-15 3:36am    
Sorry I can't help you with that, I am not the person who wrote the above code.
webmaster442 21-Dec-15 10:12am    
Corrected the code with the missing line
BillWoodruff 20-Dec-15 14:28pm    
Just curious why webmaster422 is not posting this.
Richard MacCutchan 21-Dec-15 3:34am    
He edited my question and added it there. I moved it as I am not a linq expert.
Well, I was holding back on posting an example using Linq, but now that my esteemed colleague, Richard MacCutchan, has posted one (it is very probable I esteem him much more than he esteems me):
C#
// assume a WinForm with FolderBrowserDialog added at design-time
using System.IO;
using System.Linq;

private void SomeButton_Click(object sender, EventArgs e)
{
    FileInfo longestFile = null;

    string filter = @"*.*"; // all directories, all files

    SearchOption searchType = SearchOption.AllDirectories;

    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
    {
        longestFile = new DirectoryInfo(folderBrowserDialog1.SelectedPath)
            .EnumerateFiles(filter, searchType)
            .OrderByDescending(fil => fil.Length)
            .FirstOrDefault();
    }

    Console.WriteLine("Longest File: {0} Length: {1}", longestFile.FullName, longestFile.Length);
}
Note: the 'SearchOption.AllDirectories flag is what makes the search recursive. Do read the MSDN docs for the interesting aspects/behaviors of 'EnumerateFiles compared to using 'GetFiles: [^]: "Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient."
 
Share this answer
 
Comments
Richard MacCutchan 21-Dec-15 3:41am    
Come, come Bill; I esteem anyone who makes the effort to answer questions on these forums.
Not that difficult, just list the files in the directory[^], and sort them into date order.

Removed webmaster442's addition since I do not know linq. Posted as a separate solution.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 20-Dec-15 11:29am    
5ed.
—SA
Stm21 20-Dec-15 11:41am    
easy for u not for me
Richard MacCutchan 20-Dec-15 11:44am    
I said, "not that difficult", which means I expect you to read the documentation, and try a few things for yourself. Please don't expect other people to write your code for you.
BillWoodruff 20-Dec-15 11:53am    
Vote changed from +5 to +3: I voted 5 for the Linq (first) answer. With only one link, and no real commentary: imho, this is a #3 post.
i think i found it

C#
var file = Directory.GetFiles("folder", "*.*").OrderByDescending(d => new FileInfo(d).CreationTime).Select(Path.GetFileNameWithoutExtension);
            var selector = from i in file
                           orderby new FileInfo(i).CreationTime descending
                           select i;
            var last = selector.FirstOrDefault();
            textBox2.Text = last;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900