我写了个小闹钟程序,需要开机自动运行,读取
设定好的闹铃时间,并且 设定一个Label的Visible
属性,以作提示。
每次双击运行都很正常,即能从XML中读出上次关闭
前设置的时间。
问题就出在:电脑刚开机自动运行,就读不出XML里的信息
了!!有人说可能是刚开机Framework 里读取XML的相关DLL文件
可能还没加载,于是我在Timer 里做延时读取XML还是不行! 以下是下载地址,以及相关代码:
http://d.namipan.com/d/2cee3be48bca52e76ee37028fb8f0d0f8eee7caad01a0700
private void timer1_Tick(object sender, EventArgs e)
{
//this.lblShow.Text = DateTime.Now.ToLongTimeString(); this.lblShow.Text = DateTime.Now.ToString()
+ Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
if (this.TopMost == false)
{ this.TopMost = true; }
flag++;
if (flag == 20)
ConfigMothod(); }
private void ConfigMothod()
{
Config.ReadFormregedit();
this.WinStartRuntoolStripMenuItem1.Checked = Config._RunOnWindowsStart;
//这部分放在 Form1_Load(object sender, EventArgs e)里 在开机运行时,有点问题
Config.ReadFormXml();
if (Config._CanDo)
{
if (Config._RingTime < DateTime.Now)
{
MessageBox.Show("您设置的闹铃已经过期!");
lblRing.Visible = false;
Config._CanDo = false;
timer2.Enabled = false;
Config.SaveToXml();
}
else
{
lblRing.Visible = Config._CanDo;
timer2.Enabled = Config._CanDo;
Ring.dt = Config._RingTime;
Ring.Message = Config._Message;
}
}
else
{
lblRing.Visible = Config._CanDo;
timer2.Enabled = Config._CanDo;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Win32;
namespace lkfClock
{
class Config
{
public static bool _RunOnWindowsStart;
public static bool _CanDo;
public static String _Message;
public static DateTime _RingTime; public static void ReadFormregedit()
{
RegistryKey hklm = Registry.LocalMachine;//需要引用 Microsoft.Win32 RegistryKey run = hklm.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
try
{
_RunOnWindowsStart = run.GetValue("lkfClock.exe") == null ? false : true; //hklm.Close();
} //注意,一定要关闭,注册表应用。
catch (Exception my) //这是捕获异常的
{
MessageBox.Show(my.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (hklm != null)
{
hklm.Close();
}
}
}
public static void ReadFormXml()
{
XmlTextReader reader;
if (File.Exists("ClockSet.xml"))
{
reader = new XmlTextReader("ClockSet.xml");
}
else
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream sm = asm.GetManifestResourceStream("lkfClock.ClockSet.xml");
reader = new XmlTextReader(sm);
}
try
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "CanDo")
{ _CanDo = reader.ReadElementString().Trim() == "1" ? true : false; }
else if (reader.Name == "RingTime")
{ _RingTime = Convert.ToDateTime(reader.ReadElementString().Trim()); }
else if (reader.Name == "Message")
{ _Message = reader.ReadElementString().Trim(); }
}
}
}
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
finally
{
if (reader != null)
reader.Close();
}
}
public static void SaveToXml()
{
XmlDataDocument doc = new XmlDataDocument();
doc.LoadXml("<ColockSet></ColockSet>");
XmlNode root = doc.SelectSingleNode("ColockSet");
XmlElement xelRing = doc.CreateElement("Ring");
XmlElement xelCanDo = doc.CreateElement("CanDo");
XmlElement xelRingTime = doc.CreateElement("RingTime");
XmlElement xelMessage = doc.CreateElement("Message");
xelCanDo.InnerText = _CanDo == true ? "1" : "0";
xelRingTime.InnerText = _RingTime.ToString();
xelMessage.InnerText = _Message;
xelRing.AppendChild(xelCanDo);
xelRing.AppendChild(xelRingTime);
xelRing.AppendChild(xelMessage);
root.AppendChild(xelRing); doc.Save("ClockSet.xml");
} }
}
设定好的闹铃时间,并且 设定一个Label的Visible
属性,以作提示。
每次双击运行都很正常,即能从XML中读出上次关闭
前设置的时间。
问题就出在:电脑刚开机自动运行,就读不出XML里的信息
了!!有人说可能是刚开机Framework 里读取XML的相关DLL文件
可能还没加载,于是我在Timer 里做延时读取XML还是不行! 以下是下载地址,以及相关代码:
http://d.namipan.com/d/2cee3be48bca52e76ee37028fb8f0d0f8eee7caad01a0700
private void timer1_Tick(object sender, EventArgs e)
{
//this.lblShow.Text = DateTime.Now.ToLongTimeString(); this.lblShow.Text = DateTime.Now.ToString()
+ Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
if (this.TopMost == false)
{ this.TopMost = true; }
flag++;
if (flag == 20)
ConfigMothod(); }
private void ConfigMothod()
{
Config.ReadFormregedit();
this.WinStartRuntoolStripMenuItem1.Checked = Config._RunOnWindowsStart;
//这部分放在 Form1_Load(object sender, EventArgs e)里 在开机运行时,有点问题
Config.ReadFormXml();
if (Config._CanDo)
{
if (Config._RingTime < DateTime.Now)
{
MessageBox.Show("您设置的闹铃已经过期!");
lblRing.Visible = false;
Config._CanDo = false;
timer2.Enabled = false;
Config.SaveToXml();
}
else
{
lblRing.Visible = Config._CanDo;
timer2.Enabled = Config._CanDo;
Ring.dt = Config._RingTime;
Ring.Message = Config._Message;
}
}
else
{
lblRing.Visible = Config._CanDo;
timer2.Enabled = Config._CanDo;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Win32;
namespace lkfClock
{
class Config
{
public static bool _RunOnWindowsStart;
public static bool _CanDo;
public static String _Message;
public static DateTime _RingTime; public static void ReadFormregedit()
{
RegistryKey hklm = Registry.LocalMachine;//需要引用 Microsoft.Win32 RegistryKey run = hklm.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
try
{
_RunOnWindowsStart = run.GetValue("lkfClock.exe") == null ? false : true; //hklm.Close();
} //注意,一定要关闭,注册表应用。
catch (Exception my) //这是捕获异常的
{
MessageBox.Show(my.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (hklm != null)
{
hklm.Close();
}
}
}
public static void ReadFormXml()
{
XmlTextReader reader;
if (File.Exists("ClockSet.xml"))
{
reader = new XmlTextReader("ClockSet.xml");
}
else
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream sm = asm.GetManifestResourceStream("lkfClock.ClockSet.xml");
reader = new XmlTextReader(sm);
}
try
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "CanDo")
{ _CanDo = reader.ReadElementString().Trim() == "1" ? true : false; }
else if (reader.Name == "RingTime")
{ _RingTime = Convert.ToDateTime(reader.ReadElementString().Trim()); }
else if (reader.Name == "Message")
{ _Message = reader.ReadElementString().Trim(); }
}
}
}
catch (Exception ex)
{ MessageBox.Show(ex.ToString()); }
finally
{
if (reader != null)
reader.Close();
}
}
public static void SaveToXml()
{
XmlDataDocument doc = new XmlDataDocument();
doc.LoadXml("<ColockSet></ColockSet>");
XmlNode root = doc.SelectSingleNode("ColockSet");
XmlElement xelRing = doc.CreateElement("Ring");
XmlElement xelCanDo = doc.CreateElement("CanDo");
XmlElement xelRingTime = doc.CreateElement("RingTime");
XmlElement xelMessage = doc.CreateElement("Message");
xelCanDo.InnerText = _CanDo == true ? "1" : "0";
xelRingTime.InnerText = _RingTime.ToString();
xelMessage.InnerText = _Message;
xelRing.AppendChild(xelCanDo);
xelRing.AppendChild(xelRingTime);
xelRing.AppendChild(xelMessage);
root.AppendChild(xelRing); doc.Save("ClockSet.xml");
} }
}
即好像:lblRing.Visible = Config._CanDo;一句没起作用