最后一个文件夹中的最后一个文件 [英] The last file inside the last folder
问题描述
你好再次
i我的电脑里有一个文件夹里面有子文件夹好像
0001
0002
0003
0004 ....
里面的最后一个文件夹我有txt文件
我怎么能把文本框中的最后一个文件名从tha最后一个文件夹...
i有这个代码
Hello again
i have a folder in my pc with subfolders inside like
0001
0002
0003
0004....
inside the last folder i have txt files
how can i place to a text box the last filename from tha last folder...
i have this code
var file = Directory.GetFiles("C:\\foldername", "*.*").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;
推荐答案
假设您希望它们按日期顺序排列,并希望获得最新文件你几乎已经把它放在你的文件列表中了:
Assuming you want them in date order, and want the "newest" file then you pretty much already have it in your "file" list:
var file = Directory.GetFiles("D:\\Temp", "*.*").Select(s => new FileInfo(s)).OrderByDescending(d => d.CreationTime);
tbResults.Text = File.ReadAllText(file.FirstOrDefault().FullName);
当我遇到像这样的编码挑战时,我总是希望看到...假设我认为将来我会使用类似的代码用于类似的目的...如果我可以创建一个小工具,事实上,我已经编写了代码过去的这个任务。
这里有一个草图,如果有一个有效的目录路径,你可以找到创建或修改的最后或第一个文件(最后写的)。我省略了代码来查找最后(或第一个)目录,因为这很容易做到,我想你已经知道如何做到这一点。
这个不完整的草图将编译,并为您提供目录中第一个创建或修改的文件。您可以将挑战/乐趣扩展到最后创建或修改:
When I come across a coding "challenge" like this, I'm always looking to see ... assuming I think I'll be using similar code for similar purposes in the future ... if I can create a small "tool", and, in fact, I have written code for this task in the past.
Here's a "sketch" of how, given a valid Directory-path you can find the last, or first, file created, or modified (last written-to). I omit code to find the last (or first) Directory, since that's so easy to do, and I think you already know how to do that.
This "incomplete sketch" will compile, and get you the first created or modified file in a Directory. You can have the challenge/pleasure of extending it to return last created, or modified:
// in NameSpace scope
public enum GetDateBy
{
FirstCreated,
LastCreated,
FirstModified,
LastModified
}
// in some Class' scope
private const string SearchAllString = ".";
public FileInfo GetFileDateBy(string directoryPath, bool recursive, GetDateBy datebyType)
{
if(! Directory.Exists(directoryPath)) throw new ArgumentException("invalid directory path");
var files = new DirectoryInfo(directoryPath).EnumerateFiles(
SearchAllString,
(recursive)
? SearchOption.AllDirectories
: SearchOption.TopDirectoryOnly
);
if (files.Count() == 0) return null;
switch (datebyType)
{
case GetDateBy.FirstCreated:
return files.OrderBy(f => f.CreationTime).First();
case GetDateBy.LastCreated:
// for you to write
break; // break is here so this will compile and run
case GetDateBy.FirstModified:
return files.OrderBy(f => f.LastWriteTime).First();
case GetDateBy.LastModified:
// for you to write
break; // break is here so this will compile and run
}
// throw error ?
return null;
}
对此代码的测试...假设您在Form上有一个名为'textBox1的TextBox,其'MultiLine属性设置为'true:
A test of this code ... assume you have a TextBox named 'textBox1 on a Form with its 'MultiLine property set to 'true:
private string format = "'Date:'MM/dd/yyyy\r\n'Time: 'H:mm:ss 'milliseconds: ' FFFFFFF\r\n'UTC: ' zzz";
string filepath = @"C:\Users\YourUserName\Desktop\SomeOuterFolder\SomeInnerFolder";
var f1 = GetFileDateBy(filepath, false, GetDateBy.FirstCreated);
if (f1 != null)
{
f1.Refresh();
textBox1.AppendText(string.Format("file first created: {0}\r\n{1}\r\n\r\n",f1.Name,f1.CreationTime.ToString(format)));
}
var f3 = GetFileDateBy(filepath, false, GetDateBy.FirstModified);
if (f3 != null)
{
f3.Refresh();
textBox1.AppendText(string.Format("file first modified\r\n{0}\r\n{1}\r\n\r\n",f3.Name,f3.LastWriteTime.ToString(format)));
}
// you should see output like this in the TextBox:
//file first created: SomeFileName.txt
//Date:09/22/2014
//Time: 22:18:17 milliseconds: 2414408
//UTC: +07:00
注意:
a。为什么在这里使用Enum?我只是简单地喜欢 Enums,并且觉得它们使代码更具可读性/可维护性/表达意图,并且很多时候,更容易扩展。
b。此方法返回一个FileInfo ...或... null
c。重新使用'EnumerateFiles而不是'GetFiles:'在某些情况下,EnumerateFiles可能更有效。有一篇非常好的CodeProject文章探讨/记录了这一点:[ ^ ]。
d。你可以轻松地将它重写为静态扩展方法。
e。想知道为什么LastAccessTime.Millisecond值是奇数值,或者#0:好吧,其他人也这样:[ ^ ]。
f。请注意在此处使用FileInfo实例上的'Refresh方法:这是为了确保文件系统管理器已刷新已缓存的FileInfo实例:[ ^ ]。
Note:
a. why use an Enum here ? I just plain like Enums, and feel they make code more readable/maintainable/expressive-of-intent, and, many times, easier to extend.
b. this method returns a FileInfo ... or ... null
c. re use of 'EnumerateFiles rather than 'GetFiles: 'EnumerateFiles may be more efficient in some cases. There's a very good CodeProject article that explores/documents this: [^].
d. you could re-write this as a static extension method, easily.
e. wonder why the LastAccessTime.Millisecond vale is some odd value, or #0: well, so do other people: [^].
f. note the use of the 'Refresh method on the FileInfo instance here: this is to ensure the file system manager has refreshed FileInfo instances it has cached: [^].
我找到了!!!!!
I found it !!!!!
string[] folders = Directory.GetDirectories(@"C:\\xxx\\", "*", SearchOption.AllDirectories);
var directory = Directory.GetDirectories(@"C:\\xxx\\", "*", SearchOption.AllDirectories).OrderByDescending(d => new DirectoryInfo(d).CreationTime).Select(Path.GetFileNameWithoutExtension);
var file = Directory.GetFiles(@"C:\\xxx\\", "*", SearchOption.AllDirectories).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;
这篇关于最后一个文件夹中的最后一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!