Click here to Skip to main content
12,955,842 members (63,045 online)
Rate this:
Please Sign up or sign in to vote.
See more:
The problem is that how to get file name of a directory which is lastly opened in C# window application. Which property is used for getting the name of file in a directory.
Posted 14-Sep-12 18:31pm
How would you define "lastly opened"? It's all about accurate definitions...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here this solution for this problem

you know that every microsoft window store the recent open file activity
which is HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs

the C# code is:-

string path = Environment.GetFolderPath(Environment.SpecialFolder.Recent);
            var files = Directory.GetFiles(path);
            DateTime[] xys = null;
            int s = 0;
            foreach (string xy in files)
            xys = new DateTime[s];
            s = 0;
            foreach (string xy in files)
                FileInfo fi = new FileInfo(xy);
                DateTime dts= fi.LastAccessTime;
                xys[s] = dts;
            List<DateTime> lst = new List<DateTime>();
            foreach(DateTime xyt in xys)
            string stse="";
            string strs = lst.Max(date => date).ToString();
                 stse= lst[2].ToString();
            catch { stse = lst[0].ToString(); };
            foreach (string xya in files)
                FileInfo fi = new FileInfo(xya);
                DateTime dts = fi.LastAccessTime;
                string stps = dts.ToString();
                DateTime stpss = Convert.ToDateTime(stps);
                DateTime ne = Convert.ToDateTime(stse);
                int result = DateTime.Compare(stpss, ne);
                if (result==0)
                    string dst = Path.GetFileName(xya);
                    textBox1.Text = dst;
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This makes no sense because there is no such concept as "open a directory". This is just a metaphor, used, for example, by file manager. But we don't know if your program is like a file manager or not. From the standpoint of the OS or your application run time, there is no such thing. You only have such thing as "current working directory" (but this is not something like a "last" one). If you need it, use this:[^].

In more general case, you just do some set of operations related to one or another directory, and want to get back to that directory after a while. In this case, just track that directory as you do this operation. Store the value in some string member field when you move to another directory. Use it later when needed.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,719
CHill60 3,550
Maciej Los 3,153
ppolymorphe 2,060
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100