Friday 25 March 2011

Find Last Modified File in a folder

Let me first explain the requriements & constraints and then arrive at the solution.
I have a folder in a designated path (here C:/Testing), in which there are a lot of files. My requirement need me to find a specific type of files (here *.txt) in the folder. Of all the files of that type, I have to choose only those files who have a specific name (here Hello) and get the one which was the latest modified.

Here we can see the folder with 6 text files.











5 of the files name contains "Hello", so they fulfill our criteria. Although the 6th file "little.txt" was latest modified of all the files, our logic should not choose it; instead it should choose "Hello4.txt" since it was the latest modified of the other 5 files, that matches our criteria.



string dirname = @"C:\Testing";
string[] txtFiles = System.IO.Directory.GetFiles(dirname,"*.txt");
DateTime lastmodified= Convert.ToDateTime("01/01/1900");
string LastModifiedfile = string.Empty;
foreach (string filenm in txtFiles)
{
if (filenm.Contains("Hello"))
{
if (File.GetLastWriteTime(filenm) > lastmodified)
{
lastmodified =
LastModifiedfile = filenm;
}
}
}
Response.Write(LastModifiedfile + File.GetLastWriteTime(filenm);" at "+lastmodified+" was last modified.");



Hence we successfully get the result as:

Dropdownlist - Adding tooltip for each listitem

There are times when you are designing a page with space constraint for the cotrols.
One such case can be with the Dropdownlist whose items may need more space then what it was allocated for. In this situation the user will not be able to see the complete text of the listitems. A solution to this situation is to add tooltip for the listitems.

Consider here, we have the Dropdownlist: 

<div style="width: 600px; margin: auto;">
<div style="width: 150px; float:left">Account Name: </div>
<div style="width: 400px; float:left">
<asp:DropDownList ID="DropDownList1" runat="server" Width="120px">
<asp:ListItem Value="1">Application Services</asp:ListItem>
<asp:ListItem Value="2">Toyota Motors Private Limited</asp:ListItem>
<asp:ListItem Value="3">Ford Motors Private Limited</asp:ListItem>
</asp:DropDownList>
</div>
</div>



Which leads us to have a dropdownlist as below.






We added the tooltip for each listitem.

foreach(ListItem _listItem in this.DropDownList1.Items)
{
_listItem.Attributes.Add("title", _listItem.Text);
}



Hence we have the Dropdownlist with tooltip as shown below: