//step 1 .设置配置文件
App.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="IsFirstRun" value="true"></add>
</appSettings>
</configuration>
//step 2.
注意引用:System.Configuration.dll
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
namespace RuiSoft.Common
{
public class IsSoftFirstRun
{
//返回true为首次启动,返回false为非首次启动
public static bool IsFirstStart()
{
string strIsFirstRun = "false";
bool isFirstRun = false;
//strIsFirstRun = ConfigurationManager.AppSettings("IsFirstRun");
strIsFirstRun = System.Configuration.ConfigurationManager.AppSettings["IsFirstRun"];
if (string.IsNullOrEmpty(strIsFirstRun) || strIsFirstRun.ToLower() != "true")
{
isFirstRun = false;
}
else
{
isFirstRun = true;
}
strIsFirstRun = "false";
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove("IsFirstRun");
config.AppSettings.Settings.Add("IsFirstRun", strIsFirstRun);
config.Save();
return isFirstRun;
}
}
}
//step 4.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadInformation();
}
public void LoadInformation()
{
if (IsFirstStart())
{
txtTest.Text = "首次启动";
}
else
{
txtTest.Text = "非首次启动";
}
}
}
//step 4.运行项目下的bin目录下的.exe文件,实现判断首次运行