【问题标题】:Check condition if the application is running for the first time after being installed检查应用程序是否在安装后第一次运行
【发布时间】:2009-03-07 16:46:02
【问题描述】:

C# 2008/3.5 SP1

我想检查应用程序是否是第一次运行。我开发了一个应用程序,一旦安装在客户端计算机上。我想检查它是否是第一次运行。

我已使用 windows 安装程序项目进行安装。

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

以上代码适用于 clickonce 开发。但是我怎样才能用 Windows 安装程序做类似的事情。

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

但是,与其使用注册表,还有更好的方法可以使用吗?

【问题讨论】:

    标签: c# deployment


    【解决方案1】:

    只需在您的应用程序设置中添加一个布尔值。最简单的方法是使用 IDE 和设置设计器。它必须是用户范围,但应用程序范围不可写。

    第一次检测时不要忘记切换值并保存设置。

    代码如下所示:

     if (Properties.Settings.Default.FirstUse)
     {
         Properties.Settings.Default.FirstUse = false;
         Properties.Settings.Default.Save();
    
         // do things first-time only
     }
    

    【讨论】:

    • 很好的解决方案!之前没想到。
    • 此解决方案的问题是,当您重新部署应用程序时,用户设置仍然存在,除非您在卸载应用程序时将其清除。因此,将 Properties.Settings.Default.FirstUse 设置为 false 将保持不变,并在重新部署后提供错误的反馈。
    • 在同一台机器上卸载重装无效
    【解决方案2】:

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

    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);
    }
    

    【讨论】:

    • @NeoWin 没有自动删除,您必须在卸载过程中手动或以编程方式进行。
    【解决方案3】:

    注册表听起来不错。您可能希望确保在将值设置为 false 之前正确执行所有首次初始化,并且您可以选择让用户在必要时重置它。

    【讨论】:

      【解决方案4】:

      您可以只在用户的帐户文件夹中存储一个文件,而不是搞乱注册表。我不记得确切的位置,但是您可以拨打电话来获取用户设置文件夹的位置。

      【讨论】:

        【解决方案5】:

        为了确定应用程序是否曾经被执行,我在可执行目录中检查文件 FirstTime.txt。我将该文件放在可执行目录中,因为我知道该目录在卸载期间被删除。因此,当重新部署应用程序时,我确信该文件不会存在,因此我将使用 静态 应用程序设置来初始配置我的用户设置,用户可以通过应用程序更改这些设置,因为他们就是这样 - 用户设置。

        当 form_closure 事件被触发时,我会保存这些用户设置。即使在以前的部署中有以前的用户设置,知道 FirstTime.txt 不存在(因此让我知道这是应用程序第一次启动),我确信用户设置在应用第一次运行时被重置为静态应用设置(当然,除非用户在关闭应用之前更改了这些设置)。

        不管怎样,这里有一段代码来检查应用程序是否已经被执行:

            /// <summary>
            /// Check if this is the first time ADDapt has ever executed
            /// </summary>
            /// <remarks>
            /// We know that ADDapt has run before with the existence of FirstTime.txt.
            /// </remarks>
            /// <returns>
            /// False - this was the first time the application executed
            /// </returns>
            /// <param name="ADDaptBinDirectory">
            /// Application base directory
            /// </param>
            public bool CheckFirstTime(String ADDaptBinDirectory)
            {
                bool bADDaptRunFirstTime = false;
        
                String FirstTimeFileName = string.Format("{0}//FirstTime.txt", ADDaptBinDirectory);
                // Find FirstTime.txt in Bin Directory
        
                if (File.Exists(FirstTimeFileName))
                    bADDaptRunFirstTime = true;
                else
                {
                    // Create FirstTime file
                }
        
                return bADDaptRunFirstTime;
            }
        
            /// <summary>
            /// Create the FirstTime file
            /// </summary>
            /// <remarks>
            /// Saving the creation date in the first time documents when the app was initially executed
            /// </remarks>
            /// <param name="FirstTimeFN">
            /// Full windows file name (Directory and all)
            /// </param>
            private void CreateFirstTimeFile(String FirstTimeFN)
            {
                FileInfo fi = new FileInfo(FirstTimeFN);
                DateTime dt = DateTime.Now;
        
                using (TextWriter w = fi.CreateText())
                {
                    w.WriteLine(string.Format("Creation Date: {0:g} ", dt));
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多