立聪堂德州十三局店 发表于 4 天前

C#通过注册表实现记着前次打开路径

直接上代码,代码参考自网络,举行了一些修改和封装,可以直接使用。
       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;
      } 使用方法参考:
            string sRegDir = getDirPathFromRegistry();
            string strPath = sRegDir;
            if (sRegDir == null || sRegDir.Length == 0)
            {
                strPath = System.IO.Directory.GetCurrentDirectory();
            } 在成功打开的位置保存目录:
                  //将目录保存到注册表
                  FileInfo fi = new FileInfo(filePath);
                  dirPathSaveToRegistry(fi.Directory.ToString()); https://i-blog.csdnimg.cn/direct/02a0b0266cac4068a000ca5f701ad346.png 
记着路径还可以接纳设置文件的方法,因为某些情况下用户可能不盼望软件对注册表举行操作。
在C#中,你可以使用设置文件或数据库来记着前次打开的路径。以下是一个简朴的示例,使用设置文件来记着前次的路径:
首先,添加一个设置文件(如果你的应用程序是Windows Forms应用程序,可以使用app.config;如果是其他范例的应用程序,可以使用其他方式,如JSON文件或者INI文件)。
<!-- app.config -->
<configuration>
<appSettings>
    <add key="LastOpenPath" value="默认路径"/>
</appSettings>
</configuration> 然后,在C#代码中读取和更新这个设置:
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"); 在你的应用程序启动时,读取这个设置项,在用户选择了一个新路径后,更新这个设置项。这样,下次应用程序启动时,就会记着前次的路径。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: C#通过注册表实现记着前次打开路径