打开一个具有动态生成的文件名并带有日期的文件 [英] Open a file that has a dynamically generated file name with date

查看:58
本文介绍了打开一个具有动态生成的文件名并带有日期的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,
如何打开动态生成的文件?
例如,这里是代码:

All,
How would I open a dynamically generated file?
For instance here is code:

StreamReader reader = File.OpenText(@"C:\Users\Iman\Downloads\tandem-win32-10-12-01-1\tandem-win32-10-12-01-1\bin\output.xml");




但是output.xml每次都是使用上一个具有日期和时间的先前进程创建的,例如:output.2011_07_06_23_34_53.t




but the output.xml is created every time with a previous process with date and time like: output.2011_07_06_23_34_53.t

推荐答案

我可能会使用类似
I would probably use something like
DirectoryInfo dir_info = new DirectoryInfo(my_file_path);
FileSystemInfo[] my_files = dir_info.GetFileSystemInfo("output.*");

将文件信息放入数组中,然后使用Array.Sort()查找最新文件并从该位置开始.

to put the file information in an array, then use Array.Sort() to find the newest file and go from there.


对于时间的字符串表示,最好使用特殊格式称为可排序" .

Prefer using special format for string representation of time called "sortable".

string time = System.DateTime.Now.ToString("s").Replace(':', '-');
// it will be formatted like 2011-06-15T13-45-30
// ':' should be removed to comply with
// the requirements for file system names

// just for example:
string fileName = string.Format("output.{0}.xml", time);



格式"s"是专门为此类设计的.您是否按字母数字顺序对文件进行排序,它将与按时间顺序相同.

参见:
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ].

—SA



The format "s" is specially designed for such things. Is you sort your files by alphanumeric order, it will be the same as the order by time.

See:
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^].

—SA


这篇关于打开一个具有动态生成的文件名并带有日期的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆