[英]How to get the exact path of notepad.exe in order to associate a file extension
I need to associate a file extension I have created “.rulog” with notepad.exe as part of a setup project installation for a windows 7 machine (it's here since we require admin privileges to write to the registry). 我需要将我创建的文件扩展名“.rulog”与notepad.exe相关联,作为Windows 7计算机的安装项目安装的一部分(因为我们需要管理员权限才能写入注册表)。
Basically I need to obtain programmatically the exact path of the notepad.exe. 基本上我需要以编程方式获取notepad.exe的确切路径。 Now, I understand that it typically lives in C:\\Windows\\system32. 现在,我知道它通常存在于C:\\ Windows \\ system32中。 This is part of PATH system environment variable, so I guess I could loop through all the PATH variables and test if “notepad.exe” exists by combining “notepad.exe” with the current path using File.Exists. 这是PATH系统环境变量的一部分,所以我想我可以循环遍历所有PATH变量,并通过使用File.Exists将“notepad.exe”与当前路径组合来测试是否存在“notepad.exe”。 However this feels very clumsy. 然而,这感觉非常笨拙。
Essentially I need to add an entry to 基本上我需要添加一个条目
Computer\HKEY_CLASSES_ROOT\.rulog\shell\open\command\
with the value of the path of notepad. 与记事本的路径的价值。
Incidentally I can see that .txt in: 顺便说一句,我可以看到.txt:
Computer\HKEY_CLASSES_ROOT\.txt\ShellNew
has a value for ItemName of ItemName的值为
“@%SystemRoot%\system32\notepad.exe,-470”
Perhaps I can just copy this value? 也许我可以复制这个值? Or is this dangerous?(eg does not exist). 或者这是危险的吗?(例如不存在)。
You can use: 您可以使用:
Environment.GetEnvironmentVariable("windir") + "\\system32\\notepad.exe";
Or even easier: 甚至更容易:
Environment.SystemDirectory + "\\notepad.exe";
That way it doesn't matter which drive the os is on. 这样,操作系统所在的驱动器无关紧要。
Copying the value with %systemroot% should be just fine. 使用%systemroot%复制值应该没问题。 If it works for the OS, it should work for you! 如果它适用于操作系统,它应该适合你!
Fool-proof solution: 万无一失的解决方案:
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!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.