在按钮事件上查询是否安装了 Office 365,然后根据 return 值执行代码 (WPF/C# app)
Querying if Office 365 is installed on button event, then executing code based on return value (WPF/C# app)
背景:我正在创建一个应用程序,允许我们的其他技术人员在一个简单的 GUI 中快速安装十几个程序。我将这些应用程序的安装程序嵌入到我的主程序中。 (不重要)
我创建了一个单独的 class 文件 (IsOfficeInstalled.cs
),当用户单击按钮安装 Office 时会调用该文件。这是该文件中的代码:
class IsOfficeInstalled
{
public static bool check()
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe");
if (key != null)
{
key.Close();
}
return key != null; }}
在我的 MainWindow.xaml
文件中,我试图调用 "IsOfficeInstalled" 函数并根据 return 值执行两个操作之一。这就是我感到困惑的地方。我的按钮事件中的代码如下所示:
private void btn_InstallOffice_Click(object sender, RoutedEventArgs e)
if (IsOfficeInstalled.check())
{
//If yes, then perform some code
}
else
{
//If no, then perform some different code
}
我需要帮助的地方是,从 "IsOfficeInstalled" 函数返回 true/false 结果,这样我在 "btn_InstallOffice_Click" 函数中的代码就会知道要执行哪个条件。
我不确定我是否理解你的问题。您在寻找简单的 if 语句吗?
private void btn_InstallOffice_Click(object sender, RoutedEventArgs e)
{
if (IsOfficeInstalled.check())
{
//If yes, then perform some code
}
else
{
//If no, then perform some different code
}
}
背景:我正在创建一个应用程序,允许我们的其他技术人员在一个简单的 GUI 中快速安装十几个程序。我将这些应用程序的安装程序嵌入到我的主程序中。 (不重要)
我创建了一个单独的 class 文件 (IsOfficeInstalled.cs
),当用户单击按钮安装 Office 时会调用该文件。这是该文件中的代码:
class IsOfficeInstalled
{
public static bool check()
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe");
if (key != null)
{
key.Close();
}
return key != null; }}
在我的 MainWindow.xaml
文件中,我试图调用 "IsOfficeInstalled" 函数并根据 return 值执行两个操作之一。这就是我感到困惑的地方。我的按钮事件中的代码如下所示:
private void btn_InstallOffice_Click(object sender, RoutedEventArgs e)
if (IsOfficeInstalled.check())
{
//If yes, then perform some code
}
else
{
//If no, then perform some different code
}
我需要帮助的地方是,从 "IsOfficeInstalled" 函数返回 true/false 结果,这样我在 "btn_InstallOffice_Click" 函数中的代码就会知道要执行哪个条件。
我不确定我是否理解你的问题。您在寻找简单的 if 语句吗?
private void btn_InstallOffice_Click(object sender, RoutedEventArgs e)
{
if (IsOfficeInstalled.check())
{
//If yes, then perform some code
}
else
{
//If no, then perform some different code
}
}