【问题标题】:How to get the exact path of notepad.exe in order to associate a file extension如何获取 notepad.exe 的确切路径以关联文件扩展名
【发布时间】:2012-02-08 23:31:29
【问题描述】:

我需要将我创建的文件扩展名“.rulog”与 notepad.exe 相关联,作为 Windows 7 机器安装项目安装的一部分(这里是因为我们需要管理员权限才能写入注册表)。

基本上我需要以编程方式获取 notepad.exe 的确切路径。现在,我知道它通常位于 C:\Windows\system32 中。这是 PATH 系统环境变量的一部分,所以我想我可以遍历所有 PATH 变量,并通过使用 File.Exists 将“notepad.exe”与当前路径组合来测试“notepad.exe”是否存在。但是,这感觉非常笨拙。

基本上我需要添加一个条目到

Computer\HKEY_CLASSES_ROOT\.rulog\shell\open\command\ 

与记事本路径的值。

顺便说一句,我可以在以下位置看到 .txt:

Computer\HKEY_CLASSES_ROOT\.txt\ShellNew

ItemName 的值为

“@%SystemRoot%\system32\notepad.exe,-470”

也许我可以复制这个值?或者这很危险?(例如不存在)。

【问题讨论】:

  • 出于好奇,为什么有些开发人员坚持为像文本文件这样简单的东西创建自己的专有文件扩展名?
  • 出于好奇,如果您知道记事本可以打开您的文件,为什么不使用等效的现有文件扩展名?
  • 很可能会编写一个应用程序来搜索这些文件类型的目录。我们已经为其他日志记录/信息提供了 .txt/.log,因此这只是对其的规定。
  • @JohnParr 在这种情况下“.ru.log”仍然可以搜索。

标签: c# .net path registry notepad


【解决方案1】:

你可以使用:

Environment.GetEnvironmentVariable("windir") + "\\system32\\notepad.exe";

或者更简单:

Environment.SystemDirectory + "\\notepad.exe";

这样,操作系统在哪个驱动器上并不重要。

【讨论】:

    【解决方案2】:

    使用 %systemroot% 复制值应该没问题。如果它适用于操作系统,它应该适用于您!

    【讨论】:

      【解决方案3】:

      万无一失的解决方案:

      string NotepadPath = Environment.SystemDirectory + "\\notepad.exe";
      if (System.IO.File.Exists(NotepadPath))
      {
          Microsoft.Win32.Registry.SetValue("HKEY_CLASSES_ROOT\\.rulog\\shell\\open\\command\\", "", NotepadPath + " %1");
      }
      else
      {
          //do something else or throw new ApplicationException("Notepad not found!");
      }
      

      【讨论】:

        猜你喜欢
        • 2022-06-23
        • 2015-05-03
        • 1970-01-01
        • 2020-08-12
        • 1970-01-01
        • 1970-01-01
        • 2010-09-27
        • 2016-05-02
        • 1970-01-01
        相关资源
        最近更新 更多