已有56人关注
在线升级模块问题 《C#开发典型模块大全(修订版)》
发表在C#图书答疑 2010-10-19
是否精华
版块置顶:
我的平台是vs2010。

在运行客户端程序是报错,System.IO.IOException: 文件“D:\mmm\NewXml\Update.xml”正由另一进程使用,因此该进程无法访问此文件。

好像是xml文件读取完,没有释放造成的,对应代码是operateclass中的SelectXML方法,请老师给解答一下,急求答案。

还有一个问题是System.FormatException: 输入字符串的格式不正确。
就是从ini文件中读取出来的intHour,intMin从字符串转为int型的过程中总报错,请老师帮帮忙!
分享到:
精彩评论 6
小科_mrkj
学分:43 LV2
2010-10-19
沙发
读者朋友:
    您好,问题回复如下:
    (1)问题是xml文件正在使用造成的,你检查一下相应的方法中读取完之后是否进行了释放,或者也可以使用Process进程强制关闭XML文件。
    (2)把INI文件中的intHour,intMin的值修改为整型的,或者在程序中扑捉一下异常。
mutou
学分:0 LV1
TA的每日心情
开心
2022-07-25 10:41:35
2010-10-20
板凳
就是XML文件在读取是调用了operateclass类中的以下这个方法。
  public Hashtable SelectXML(string strPath) 
        {
            Hashtable HTable = new Hashtable();
            XmlDocument doc = new XmlDocument();
            doc.Load(strPath);

            //获取NewDataSet节点的所有子节点
            XmlNodeList xnl = doc.SelectSingleNode("UpdCfg").ChildNodes;
            string strVersion = "";
            string strInfo = "";
            foreach (XmlNode xn in xnl)//遍历UpdCfg节点下的所有子节点
            {
                XmlElement xe = (XmlElement)xn;     //将子节点类型转换为XmlElement类型
                if (xe.Name == "Upgrade")           //判断节点名为Upgrade
                {
                    XmlNodeList xnlChild = xe.ChildNodes;   //继续获取xe子节点的所有子节点
                    foreach (XmlNode xnChild in xnlChild)  //遍历Upgrade节点下的所有子节点
                    {
                        XmlElement xeChild = (XmlElement)xnChild;      //转换类型
                        if (xeChild.Name == "Version")
                        {
                            strVersion = xeChild.InnerText;
                        }
                        if (xeChild.Name == "UpdInfo")
                        {
                            strInfo = xeChild.InnerText;
                        }
                    }
                    HTable.Add(strVersion, strInfo);
                }
            }
            doc.Save(strPath);

            return HTable;
        }

读取xml文件时,用的是XMLDocument的Load方法读取的,如何释放呢,没有找到相关的方法。
在客户端的主窗体加载时,调用了ControlUpdate() 方法。
private void ControlUpdate() 
        {

            HNativeTable = operateclass.SelectXML(strOldXmlName);
            HServerTable = operateclass.SelectXML(strNewXmlName);
            IDictionaryEnumerator IDEnumeratorNative = HNativeTable.GetEnumerator();
            IDictionaryEnumerator IDEnumeratorServer = HServerTable.GetEnumerator();
            string str = "";
            while (IDEnumeratorNative.MoveNext())
            {
                if (str == "")
                    str = IDEnumeratorNative.Key.ToString();
                else
                    str += "\n" + IDEnumeratorNative.Key.ToString(); 
            }
            //获取本地机器的最新版本
            if (str.IndexOf("\n") != -1)
                strOldVersion = str.Substring(str.LastIndexOf("\n") + 1, str.Length - str.LastIndexOf("\n") - 1);
            else
                strOldVersion = str;
            //使用本地机器上的最新版本与服务器上的新版本进行比较
            while (IDEnumeratorServer.MoveNext())
            {
                if (string.Compare(strOldVersion, IDEnumeratorServer.Key.ToString()) < 0)
                    list.Add(IDEnumeratorServer.Key.ToString());
            }
            if (list.Count > 0)
            {
                ClientUpdateForm clientUpdateForm = new ClientUpdateForm();
                clientUpdateForm.Show();
            }
            else
            {
                MessageBox.Show("当前版本已经是最新版本,不需要升级!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

其中的      HNativeTable = operateclass.SelectXML(strOldXmlName);
            HServerTable = operateclass.SelectXML(strNewXmlName);
就是导致问题的所在,由于第一次调用SelectXML时,xml文件没有关闭,导致第二次无法读取xml文件,这个问题,我没有找到解决方法,您给我一点代码提示好吗?
而我对进程那块不太懂,您能否附上相应的代码或者释放XML文件的解决办法。
多谢老师了,我都纠结了两天了,拜托老师了!
小科_mrkj
学分:43 LV2
2010-10-20
地板
读者朋友:
    您好,杀死相关进程的代码如下:
Process Pro=new Process();
foreach(Process p in Process.GetProcessesByName("进程名"))
{
if(!p.CloseMainWindow())
{
p.Kill();
}
}
用这种方法杀掉XML进程,然后再运行程序。
mutou
学分:0 LV1
TA的每日心情
开心
2022-07-25 10:41:35
2010-10-21
4L
太感谢老师了,这个问题终于解决了!看来我要好好学习进程和线程这块,挺重要的

永远的小六
学分:0 LV1
TA的每日心情
h
2022-02-01 22:04:17
2011-02-28
5L
[FIELDSET][LEGEND]引自:3楼[/LEGEND]
读者朋友:
    您好,杀死相关进程的代码如下:
Process Pro=new Process();
foreach(Process p in Process.GetProcessesByName("进程名"))
{
if(!p.CloseMainWindow())
{
p.Kill();
}
}
用这种方法杀掉XML进程,然后再运行程序。
[/FIELDSET]

回复:

小科老师,这个怎么用啊。 放哪里?能再讲一下么?
  
永远的小六
学分:0 LV1
TA的每日心情
h
2022-02-01 22:04:17
2011-02-28
6L
小科老师的办法。杀死进程 这个进程名是 怎么确定啊,我打开任务管理器,把进程名字写进去么?
!p.CloseMainWindow() 这个是什么意思?

求解。小科老师,麻烦您了
首页上一页 1 下一页尾页 6 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照