Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 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
Comments
Sergey Alexandrovich Kryukov 15-Sep-12 1:57am    
How would you define "lastly opened"? It's all about accurate definitions...
--SA

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)
            {
                s++;
            }
            xys = new DateTime[s];
            s = 0;
            foreach (string xy in files)
            {
                FileInfo fi = new FileInfo(xy);
                DateTime dts= fi.LastAccessTime;
                xys[s] = dts;
                s++;
            }

            List<DateTime> lst = new List<DateTime>();
            foreach(DateTime xyt in xys)
            {
              lst.Add(xyt);
            }
            lst.Sort();
            lst.Reverse();
            string stse="";
            string strs = lst.Max(date => date).ToString();
            try
            {
                 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;
                    break;
                }

            }
 
Share this answer
 
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:
http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory%28v=vs.110%29.aspx[^].

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.

—SA
 
Share this answer
 

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