带文件名的文本框

带文件名的文本框

本文介绍了带文件名的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计算机和内部文件夹中有一个文件夹,我有许多由其他程序创建的txt文件。

i想要创建一个文本框,捕获创建程序的最后一个txt文件的文件名。

i知道我很难做到这一点

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

推荐答案

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;


// 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);
}

注意:'SearchOption.AllDirectories标志是使搜索递归的原因。请阅读MSDN文档,了解'EnumerateFiles与使用'GetFiles相比的有趣方面/行为:[]:因此,当您使用许多文件和目录时,EnumerateFiles可以更有效。

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."



这篇关于带文件名的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:22