Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");



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
Posted
Updated 9-Jul-11 16:43pm
v2
Comments
Emrah Taylan 9-Jul-11 16:14pm    
Do you want to read file same second?

I would probably use something like
DirectoryInfo dir_info = new DirectoryInfo(my_file_path);
FileSystemInfo[] my_files = dir_info.GetFileSystemInfo("output.*");
to put the file information in an array, then use Array.Sort() to find the newest file and go from there.
 
Share this answer
 
Comments
Espen Harlinn 10-Jul-11 7:33am    
That would work, nice and simple - my 5
Prefer using special format for string representation of time called "sortable".

C#
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);


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
 
Share this answer
 
v3
Comments
Espen Harlinn 10-Jul-11 7:34am    
Good point, my 5
Sergey Alexandrovich Kryukov 10-Jul-11 13:34pm    
Thank you, Espen.
--SA
Sergey Alexandrovich Kryukov 12-Jul-11 16:25pm    
Thank you for fixing my text (I did not see where, I think I can trust you :-).
--SA
Espen Harlinn 12-Jul-11 16:51pm    
I just removed a single character :)

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