做的一个Winforms小闹钟程序时,出现一个非常莫名其妙的故障:
我是将闹钟信息保存于XML文件中,当我保存闹响的音乐文件的路径时候,发现无法保存,并且不报错!且之后也不能再保存闹钟信息!!但是如果不设置闹响音乐文件路径的话,则没有问题!
代码:
1
public class AlertsManager:List<Alert>
2
{
3
private string xmlFileName="\Alerts.xml";
4
private XmlDocument docFile;
5
private List<string> exceptions=new List<string>();
6


7
public new void Add(Alert alert)
8
{
9
if(docFile.SelectNodes("//Alert[@Time='" + alert.Time.ToLongTimeString()+"']").Count<=0)
10
{
11
XmlElement newAlert=docFile.CreateNode(XmlNodeType.Element, "Alert", null) as XmlElement;
12
XmlElement newAlertMusicFile = docFile.CreateElement("MusicFilePath");
13

14
newAlertMusicFile.InnerXml = "<![CDATA[" + alert.MusicFile + "]]>";
15

16
newAlert.AppendChild(newAlertMusicFile);
17

18
docFile.DocumentElement.AppendChild(newAlert);
19
docFile.Save(xmlFileName);
20
base.Add(alert);
21


后来发现CSDN上还有一个人和我遇到了同样的情况(他保存图片路径),后来他也解决了.
我按照其解决办法,将
private string xmlFileName=\\Alerts.xml;
改成了
private string xmlFileName = Application.StartupPath+@"\Alerts.xml";
问题解决!!!
希望大家记住,避免发生这种怪错误.