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");

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

相关推荐
csdn_aspnet17 分钟前
浅谈 C# 与 Data URI
c#
烛阴2 小时前
C# 正则表达式:量词与锚点——从“.*”到精确匹配
前端·正则表达式·c#
sbjdhjd2 小时前
开源分享 | 超浪漫 3D 圣诞树立体动画(附零基础使用教程)
3d·青少年编程·开源·html·节日
云中飞鸿3 小时前
值类型、引用类型
c#
布茹 ei ai3 小时前
城市天气查询系统 (City Weather Dashboard)
javascript·vue.js·html·css3·开源软件·天气预报
跟着珅聪学java3 小时前
在JavaScript中清空一个div的内容有多种方法,以下是常用的几种实现方式及适用场景:
html
软件技术NINI4 小时前
娃娃店html+css 4页
前端·css·html
c#上位机5 小时前
halcon窗口显示文字
图像处理·c#·halcon
kingwebo'sZone6 小时前
Datagridview 显示当前选中行
c#
时光追逐者7 小时前
一个基于 .NET 开源、功能强大的分布式微服务开发框架
分布式·微服务·开源·c#·.net·.net core