Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone!

I need a command to search latest modified date file (ex. *.txt) and copy text from the file to textbox.

Please help me for that command
Thank you!

What I have tried:

I tried that command but i can see latest file without search (ex. *.txt), and cannot copy text from the file to my textbox.

Dim myDirectory As New IO.DirectoryInfo("C:\TestFolder")
Dim myfiles() As String = myDirectory.GetFiles.OrderByDescending(Function(x) x.LastWriteTime).Select(Function(x) x.FullName).Take(40).ToArray

MsgBox(String.Join(Environment.NewLine, myfiles))
Posted
Updated 22-Jun-21 2:22am
Comments
Richard MacCutchan 22-Jun-21 7:22am    
Where is the code to copy the text?
ionMEMBER 22-Jun-21 7:27am    
from notepad (.txt) to textbox or richtexbox
Richard MacCutchan 22-Jun-21 7:38am    
And you think anyone here can guess what that is supposed to mean?
ionMEMBER 22-Jun-21 7:53am    
If i have some files .txt at C:\TestFolder and lastest modified data is ex. 123.txt then, copy text from that to textbox or richtextbox
Richard MacCutchan 22-Jun-21 8:02am    
What is the problem? You just need to use the File Class (System.IO) | Microsoft Docs[^] to read the file (using something like ReadAllLines or ReadAllText), and set the result into the Text property of the text box.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

All you have done so far is construct a list of files of all types orders by last write time.
You need to add code to filter them to just "*.txt" files - Path.GetExtension Method (System.IO) | Microsoft Docs[^] will help you with that.
Then get the first such file: Enumerable.FirstOrDefault Method (System.Linq) | Microsoft Docs[^] will help there.
Then read it, and set the textbox. File.ReadAllLines Method (System.IO) | Microsoft Docs[^] should be useful, as will the TextBoxBase.Lines Property (System.Windows.Forms) | Microsoft Docs[^]

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
ionMEMBER 22-Jun-21 7:49am    
I apologize if I bothered you and thank you for your help
If i understand you well, you want to get only those files where extension is *.txt. Then use Directory.GetFiles Method (System.IO) | Microsoft Docs[^] with searchPattern parameter.

VB.NET
Dim myfiles As String = String.Join(Environment.NewLine, _
    Directory.GetFiles("C:\TestFolder", "*.txt") _
        .Select(Function(x) New FileInfo(x)) _
        .OrderByDescending(Function(x) x.LastWriteTime) _
        .Select(Function(x) x.FullName))


[EDIT]
VB.NET
Dim newestfile As String = Directory.GetFiles("D:\", "*.txt") _
        .Select(Function(x) New FileInfo(x)) _
        .OrderByDescending(Function(x) x.LastWriteTime) _
        .Select(Function(x) x.FullName) _
		.First()
Me.TextBox1.Text = newestfile
 
Share this answer
 
v4
Comments
ionMEMBER 22-Jun-21 7:48am    
Now i can search *.txt from this folder. Thank you so much but i need and to copy text from latest modified data .txt to textbox. Is that a code for that?
Maciej Los 22-Jun-21 8:08am    
If you want to display text grabbed in Linq query onto textbox, use Me.TextBox1.Text = myfiles
ionMEMBER 22-Jun-21 8:11am    
how to see only last row because is C:\TestFolder\dd.txtC:\TestFolder\aa.txt
Maciej Los 22-Jun-21 8:12am    
What you mean by saying "last row"? Do you mean the text file with the latest write time?
ionMEMBER 22-Jun-21 8:13am    
yes ex. only C:\TestFolder\dd.txt

dd.text 22/06/2021 14:03
aa.txt 22/06/2021 13:15
Try something like:
VB
Dim myfiles() As String = Directory.GetFiles("C:\TestFolder", "*.txt") _
    .Select(Function(x) New FileInfo(x)) _
    .OrderByDescending(Function(x) x.LastWriteTime) _
    .Select(Function(x) x.FullName).ToArray

For Each path As String in myFiles
    Dim readText As String = File.ReadAllText(path)
    TextBox1.Text += readText
Next
 
Share this answer
 
Comments
ionMEMBER 22-Jun-21 8:32am    
I see on the textbox1, all texts on all files .txt not only latest modified data file text .txt
Richard MacCutchan 22-Jun-21 8:40am    
Well you have to decide which files from the list that you want.
ionMEMBER 22-Jun-21 8:42am    
ok thank you so much
Maciej Los 22-Jun-21 9:09am    
5ed!
Richard MacCutchan 22-Jun-21 9:17am    
Thanks, but I did crib some of your code.

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