安装后检查应用程序是否首次运行 [英] Check condition if the application is running for the first time after being installed

查看:77
本文介绍了安装后检查应用程序是否首次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#2008 / 3.5 SP1

C# 2008/3.5 SP1

我想检查一下应用程序是否是第一次运行。我已经开发了一个应用程序,并将其安装在客户端计算机上。我想检查一下是否是第一次运行。

I want to check to see if the application is running for the first time. I have developed an application and once this is installed on the clients computer. I want to make a check if it is first time running.

我已使用Windows Installer项目进行安装。

I have installed using the windows installer project.

 if (System.Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun)
 {
      // Do something here
 }

以上代码适用于clickonce开发。但是我该如何用Windows安装程序做类似的事情。

The above code works for a clickonce development. But how can I do something similar with a windows installer.

我当时在考虑安装应用程序时添加一个注册表。然后,在程序首次运行时检查此注册项(true)。首次运行后,将注册表编辑为(false)。

I was thinking of added a registery when the application installs. Then check this registery item, when the program runs for the first time (true). Once it has run for the first time edit the registry to (false).

但是,不是使用注册表,还有更好的方法吗?

However, rather then use the registry, is there a better method that I can use?

推荐答案

在注册表之外存储应用程序数据的一个好地方是应用程序数据文件夹。如果您是在首次运行时创建此目录,则只需在后续加载中对其进行测试。同样,如果您的应用程序需要它,那么您将拥有一个很好的数据存储位置。

A good place to store application data outside of the registry is the application data folder. If you create this directory on first run then you would just need to test for it on subsequent loads. Also if your application requires it you then have a good storage location for data.

string data = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string name = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
string path = Path.Combine(data, name);

if (Directory.Exists(path))
{
  // application has been run
}
else
{
  // create the directory on first run
  DirectoryInfo di = Directory.CreateDirectory(path);
}

这篇关于安装后检查应用程序是否首次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆