Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 19:31pm
Comments
Sergey Alexandrovich Kryukov at 15-Sep-12 1:57am
   
How would you define "lastly opened"? It's all about accurate definitions...
--SA
Rate this: bad
good
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:
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
  Permalink  
Rate this: bad
good
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)
            {
                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;
                }
 
            }
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 214
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 163
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2014
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