C#通过注册表实现记住上次打开路径

直接上代码,代码参考自网络,进行了一些修改和封装,可以直接使用。

cs 复制代码
       private bool dirPathSaveToRegistry(string path)
        {
            try
            {
                Microsoft.Win32.RegistryKey software = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE", true);   //打开注册表中的software       
                Microsoft.Win32.RegistryKey ReadKey = software.OpenSubKey("FolderPath", true);                              //打开自定义的文件目录项
                if (ReadKey == null)
                {
                    ReadKey = software.CreateSubKey("FolderPath");      //不存在则新建
                    ReadKey?.SetValue("OpenFolderDir", path);           //写入文件目录
                    ReadKey?.Close();
                    Microsoft.Win32.Registry.CurrentUser.Close();
                }
                else
                {
                    ReadKey.SetValue("OpenFolderDir", path);
                    ReadKey.Close();
                    Microsoft.Win32.Registry.CurrentUser.Close();
                }
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        private string getDirPathFromRegistry()
        {
            try
            {
                Microsoft.Win32.RegistryKey software = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE", true);
                Microsoft.Win32.RegistryKey ReadKey = software.OpenSubKey("FolderPath", true);

                string path = ReadKey?.GetValue("OpenFolderDir")?.ToString();
                return path;
            }
            catch (Exception)
            {
                //TO DO
            }

            return null;
        }

使用方法参考:

cs 复制代码
            string sRegDir = getDirPathFromRegistry();
            string strPath = sRegDir;
            if (sRegDir == null || sRegDir.Length == 0)
            {
                strPath = System.IO.Directory.GetCurrentDirectory();
            }

在成功打开的位置保存目录:

cs 复制代码
                    //将目录保存到注册表
                    FileInfo fi = new FileInfo(filePath);
                    dirPathSaveToRegistry(fi.Directory.ToString());

记住路径还可以采用配置文件的方法,因为某些情况下用户可能不希望软件对注册表进行操作。

在C#中,你可以使用配置文件或数据库来记住上次打开的路径。以下是一个简单的示例,使用配置文件来记住上次的路径:

首先,添加一个配置文件(如果你的应用程序是Windows Forms应用程序,可以使用app.config;如果是其他类型的应用程序,可以使用其他方式,如JSON文件或者INI文件)。

XML 复制代码
<!-- app.config -->
<configuration>
  <appSettings>
    <add key="LastOpenPath" value="默认路径"/>
  </appSettings>
</configuration>

然后,在C#代码中读取和更新这个配置:

cs 复制代码
using System.Configuration;
 
// 读取上次保存的路径
string lastOpenPath = ConfigurationManager.AppSettings["LastOpenPath"];
 
// 更新路径
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["LastOpenPath"].Value = "新路径";
config.Save(ConfigurationSaveMode.Modified);
 
// 强制重新加载配置
ConfigurationManager.RefreshSection("appSettings");

在你的应用程序启动时,读取这个配置项,在用户选择了一个新路径后,更新这个配置项。这样,下次应用程序启动时,就会记住上次的路径。

相关推荐
SunflowerCoder30 分钟前
EF Core + PostgreSQL 配置表设计踩坑记录:从 23505 到 ChangeTracker 冲突
数据库·postgresql·c#·efcore
阿蒙Amon3 小时前
C#每日面试题-常量和只读变量的区别
java·面试·c#
我是唐青枫3 小时前
C#.NET ConcurrentBag<T> 设计原理与使用场景
c#·.net
大王小生4 小时前
C# CancellationToken
开发语言·c#·token·cancellation
listhi5204 小时前
基于C#实现屏幕放大镜功能
开发语言·c#
bjzhang756 小时前
使用 HTML + JavaScript 实现在线知识挑战
前端·javascript·html
该用户已不存在7 小时前
不止是初始化,4个C# 构造函数解析与实例
后端·c#·.net
松涛和鸣8 小时前
DAY52 7-Segment Display/GPIO/Buttons/Interrupts/Timers/PWM
c语言·数据库·单片机·sqlite·html
冴羽9 小时前
2025 年 HTML 年度调查报告公布!好多不知道!
前端·javascript·html
无风听海10 小时前
深入讲解 C# 中 string 如何支持 CultureInfo
开发语言·c#