简体   繁体   English

如何获取notepad.exe的确切路径以便关联文件扩展名

[英]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.

相关问题 以Notepad.exe的方式打开文件的标志是什么? - Which flags to open a file the way Notepad.exe does? 如何在Windows应用程序中以隐藏模式启动notepad.exe? - How to start notepad.exe in hidden mode in windows application? 用新的替换notepad.exe - Replacing notepad.exe with a new one 使用notepad.exe在列表视图中打开活动项目 - Opening active item in a listview with notepad.exe 使用Awesomium从html按钮运行notepad.exe - Run notepad.exe from a html-button using Awesomium 如何在记事本中打开文件的文件名和路径? - How to get filename and path of file opened in Notepad? 如何在 C# windows 应用程序的表单加载事件中的 process.start("notepad.exe") 之前播放 windows 媒体播放器 - how to play windows media player before the process.start("notepad.exe") in form load event in C# windows app C# 为什么 Process.Start("notepad.exe" myFile) 正常工作,而 Process.Start("notepad++.exe" myFile) 不工作 - C# Why does Process.Start("notepad.exe" myFile) is working and Process.Start("notepad++.exe" myFile) is not working System.Diagnostics.Process.Start(“ Notepad.exe”); 在实时服务器上不工作 - System.Diagnostics.Process.Start(“Notepad.exe”); is not working on live server Process.Start with UseShellExecute 在前一个关闭窗口后无法将 notepad.exe 窗口带到前台 - Process.Start with UseShellExecute fails to bring notepad.exe window to the foreground after a previous close window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM