我尝试通过调用以下代码来获取 AcroRd32.exe 的路径:
public static string acrobatPath = Registry.GetValue(@"HKEY_CLASSES_ROOT\Applications\AcroRD32.exe\shell\Read\command", "", 0).ToString();
我收到的是正确的值:
"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe" "%1"
但我只想要 AcroRd32.exe 的路径,不带“%1”。 我现在可以使用 split 命令:
public static string acrobatPath = Registry.GetValue(@"HKEY_CLASSES_ROOT\Applications\AcroRD32.exe\shell\Read\command", "", 0).ToString();
string[] split = new string[2];
split = acrobatPath.Split('"');
// mask path with ""
acrobatPath = "\"" + split[1] + "\""; //get only path
但由于静态属性,acrobatPath 值无法更改。
我也不能使用 substr() 因为路径可能不同,例如如果末尾没有参数(“%1”)。
如何一次性提取路径并设置静态变量?
最佳答案
为您的类使用静态构造函数,并在那里完成字符串操作的所有工作。
class YourClass
{
public static string acrobatPath;
// This static constructor will be called before first access to your type.
static YourClass()
{
acrobatPath = Registry.GetValue(@"HKEY_CLASSES_ROOT\Applications\AcroRD32.exe\shell\Read\command", "", 0).ToString();
string[] split = new string[2];
split = acrobatPath.Split('"');
// mask path with ""
acrobatPath = "\"" + split[1] + "\""; //get only path
}
}
关于C# 从注册表中获取不带参数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8308308/