如何获取最后打开的目录的文件名 [英] How to get file name of a directory which is lastly opened

查看:142
本文介绍了如何获取最后打开的目录的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是如何获取最后在C#窗口应用程序中打开的目录的文件名.哪个属性用于获取目录中的文件名.

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.

推荐答案

这没有任何意义,因为没有打开目录"这样的概念.这只是一个隐喻,例如由文件管理器使用.但是我们不知道您的程序是否像文件管理器.从操作系统或应用程序运行时的角度来看,没有这种事情.您只有诸如当前工作目录"之类的东西(但这不像最后一个"目录).如果需要,请使用以下内容:
http://msdn.microsoft.com/zh-cn/library/system.io.directory.getcurrentdirectory%28v = vs.110%29.aspx [
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


此处是此问题的解决方法

您知道每个Microsoft窗口都会存储最近打开的文件活动
这是HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ RecentDocs


C#代码为:-


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;
                }

            }


这篇关于如何获取最后打开的目录的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆