我开发了一个应用程序并将其安装在客户端计算机上.在我的应用程序中,我需要获取其安装路径.我的应用程序有一个注册表项:
HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\[AppPath]
Run Code Online (Sandbox Code Playgroud)
如何AppPath使用C#阅读?
Mig*_*ell 76
string InstallPath = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\MyApplication\AppPath", "Installed", null);
if (InstallPath != null)
{
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
该代码应该得到你的价值.你需要做到
using Microsoft.Win32;
Run Code Online (Sandbox Code Playgroud)
为了那个工作.
Jav*_*ram 28
见http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C
更新:
您可以RegistryKey在Microsoft.Win32命名空间下使用类.
一些重要的功能RegistryKey如下:
GetValue //to get value of a key
SetValue //to set value to a key
DeleteValue //to delete value of a key
OpenSubKey //to read value of a subkey (read-only)
CreateSubKey //to create new or edit value to a subkey
DeleteSubKey //to delete a subkey
GetValueKind //to retrieve the datatype of registry key
Run Code Online (Sandbox Code Playgroud)
您可以使用以下内容来获取注册表认为安装的位置:
(string)Registry.LocalMachine.GetValue(@"SOFTWARE\MyApplication\AppPath",
"Installed", null);
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用以下内容查找应用程序实际启动的位置:
System.Windows.Forms.Application.StartupPath
Run Code Online (Sandbox Code Playgroud)
如果您尝试将该.exe位置用作查找相关文件的相对路径,则后者比前者更可靠.用户可以在安装后轻松移动东西,并且仍然可以使应用程序正常工作,因为.NET应用程序不依赖于注册表.
使用StartupPath,你甚至可以做一些聪明的象有您的应用程序更新在运行时的注册表项,而不是由于缺少/错误/损坏的条目惨败崩溃.
并确保将应用程序设置功能视为值的存储而不是注册表(Properties.Settings.Default.mySettingEtc).您可以读取/写入应用程序和/或用户级别的设置,这些设置将保存为MyApp.exe.config标准位置的简单文件.应用程序安装/删除的过去(好旧的Win 3.1/DOS天)的一个很好的爆炸是一个或两个文件夹结构的简单复制/删除,而不是一些令人费解,神秘的安装/卸载例程,留下各种垃圾在注册表中,洒在硬盘上.
如果您希望将其转换为特定类型,则可以使用此方法.默认情况下,大多数非基本类型都不支持直接转换,因此您必须相应地处理这些类型.
public T GetValue<T>(string registryKeyPath, string value, T defaultValue = default(T))
{
T retVal = default(T);
retVal = (T)Registry.GetValue(registryKeyPath, value, defaultValue);
return retVal;
}
Run Code Online (Sandbox Code Playgroud)