日期:2014-05-18  浏览次数:20858 次

C# WinForm窗体怎样判断是第一次启动应用程序!求方法或代码!
C# WinForm窗体怎样判断是第一次启动应用程序!求方法或代码!

------解决方案--------------------
C# code

        /// <summary>
        /// 写注册表中字串(REG_SZ)数据
        /// </summary>
        /// <param name="HKLM_SOFT_Dir_Name">HKLM\SOFTWARE的子项名称</param>
        /// <param name="Sub_Dir_Name">\SOFTWARE\SOFT_Dir_Name\的子项的名称</param>
        /// <param name="Key_Name">Sub_Dir_Name的子键名</param>
        /// <param name="Key_Value">写入Key_Name的键值</param>
        public void SetRegeditData(String HKLM_SOFT_Dir_Name, String Sub_Dir_Name, String Key_Name, String Key_Value)
        {
            RegistryKey hklm = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
            RegistryKey software;
            if (IsRegeditDirExist(HKLM_SOFT_Dir_Name))
            {
                software = hklm.OpenSubKey(HKLM_SOFT_Dir_Name, true);
            }
            else
            {
                software = hklm.CreateSubKey(HKLM_SOFT_Dir_Name);
            }
            RegistryKey aimdir = software.CreateSubKey(Sub_Dir_Name);
            aimdir.SetValue(Key_Name, Key_Value);
        }


        /// <summary>
        /// 判断目录是否存在
        /// </summary>
        /// <param name="HKLM_SOFT_Dir_Name">HKLM\SOFTWARE下是否存在该子项</param>
        /// <returns>true or false</returns>
        public bool IsRegeditDirExist(String HKLM_SOFT_Dir_Name)
        {
            bool _exit = false;
            string[] subkeyNames;
            RegistryKey hklm = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
            subkeyNames = hklm.GetSubKeyNames();
            foreach (string DirName in subkeyNames)
            {
                if (DirName == HKLM_SOFT_Dir_Name)
                {
                    _exit = true;
                    return _exit;
                }
            }
            return _exit;
        }

------解决方案--------------------
... 用什么注册表,App.config就可以了嘛。

C# code

string strIsFirstRun = "false";
bool isFirstRun = false;
strIsFirstRun = 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();