i created one windows app by using c#.net and now one add one more form in that i wanted to get all the unused software or files by using c#.net
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Wim SturkenboomPosted Nov 21, 2014, 5:37 AM
Below code does it slightly different, it loops through specified directories and add files older than a certain date to a datatable. The datatable needs to contain at least 2 columns for file name and last accessed date.
You can use the datatable as a data source for the list box.
///
/// traverse directory recursively and find 'old' files
///
/// directory
/// reference date
/// datatable to store file names and dates
///
private bool traverseDirectory(string strDirectory, DateTime dtReference, DataTable tblResults)
{
// find directories and process them
string[] directories = Directory.GetDirectories(strDirectory);
for (int cnt = 0; cnt < directories.Length; cnt++)
{
traverseDirectory(directories[cnt], dtReference, tblResults);
}
// find files and process them
string[] files = Directory.GetFiles(strDirectory, "*.exe");
for (int cnt=0; cnt < files.Length; cnt++)
{
DateTime lastaccesstime = File.GetLastAccessTime(files[cnt]);
if(lastaccesstime<=dtReference)
{
tblResults.Rows.Add(files[cnt], lastaccesstime);
}
}
return true;
}
Note: it's strongly advised to use a backgroundworker as it might be slow and the UI becomes unresponsive.
srikanth RadharamPosted Nov 21, 2014, 12:02 AM
Wim SturkenboomPosted Nov 20, 2014, 1:22 PM
Example code:
FileInfo fi = new FileInfo("yourfile.exe");
Console.WriteLine("Last access " + fi.LastAccessTime);
Embed it in a program that recursively loops through all directories, finds exe files and check the lastaccesstime.
Note:
You might have to run the below command in a command prompt with admin privileges to enable changes of the lastaccesstime on the NTFS filesystem