ApplicationDeployment.IsFirstRun 属性

定义

获取一个值,该值指示此应用程序是否是第一次在客户端计算机上运行。Gets a value indicating whether this is the first time this application has run on the client computer.

public:
 property bool IsFirstRun { bool get(); };
public bool IsFirstRun { get; }
member this.IsFirstRun : bool
Public ReadOnly Property IsFirstRun As Boolean

属性值

Boolean

如果此版本的应用程序此前未在客户端计算机上运行过,则为 true;否则为 falsetrue if this version of the application has never run on the client computer before; otherwise, false.

示例

下面的代码示例使用 IsFirstRun 来确定是否检查文件组的更新 HelpFilesThe following code example uses IsFirstRun to decide whether to check for an update to the file group HelpFiles. 如果一个或多个文件已更新,则它将调用 DownloadFileGroup 以获取新版本。If one or more of the files have been updated, it calls DownloadFileGroup to obtain the new versions.

private void DownloadFileGroupSync(string fileGroup)
{
    if (ApplicationDeployment.IsNetworkDeployed)
    {
        ApplicationDeployment deployment = ApplicationDeployment.CurrentDeployment;

        if (deployment.IsFirstRun)
        {
            try
            {
                if (deployment.IsFileGroupDownloaded(fileGroup))
                {
                    deployment.DownloadFileGroup(fileGroup);
                } 
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show("This application is not a ClickOnce application. Error: " + ioe.Message);
                return;
            }

            downloadStatus.Text = String.Format("Download of file group {0} complete.", fileGroup);
        }
    }            
}

Private Sub DownloadFileGroupSync(ByVal fileGroup As String)
    If (ApplicationDeployment.IsNetworkDeployed) Then
        Dim deployment As ApplicationDeployment = ApplicationDeployment.CurrentDeployment

        If (deployment.IsFirstRun) Then
            Try
                If (deployment.IsFileGroupDownloaded(fileGroup)) Then
                    deployment.DownloadFileGroup(fileGroup)
                End If
            Catch ioe As InvalidOperationException
                MessageBox.Show("This application is not a ClickOnce application. Error: " & ioe.Message)
                Exit Sub
            End Try

            DownloadStatus.Text = String.Format("Download of file group {0} complete.", fileGroup)
        End If
    End If
End Sub

注解

使用 IsFirstRun 确定是否需要在首次运行应用程序时执行任何一次性的初始化操作。Use IsFirstRun to determine whether you need to perform any one-time initialization operations the first time your application runs.

不要假设 IsFirstRun 设置为 true 指示应用程序的这一实例是向用户显示其用户界面的第一个实例。Do not assume that a IsFirstRun setting of true indicates that this instance of an application is the first one to show its user interface to the user. 由于系统计划进程的方式,应用程序的一个实例可能会 IsFirstRun 将其属性设置为 true ,而另一个实例可能是第一个在屏幕上呈现的实例。Due to the way the system schedules processes, one instance of the application may have its IsFirstRun property set to true, but another instance may be the first to render on the screen. 每当用户从一个版本升级到下一个版本时,此属性的值就会重置。The value of this property is reset whenever the user upgrades from one version to the next. 如果只想在运行应用程序的任何版本时执行操作,则需要执行其他测试,如检查是否存在首次创建的文件,或使用 应用程序设置 存储标志。If you want to perform an operation only the very first time any version of the application is run, you will need to perform an additional test, such as checking for the existence of a file you created the first time, or storing a flag using Application Settings.

适用于