各位老大,希望帮小弟一把。。
我用xmldocument类 这样加载一个文件
xmlDoc = new XmlDocument();
xmlDoc.Load("test.xml");
因为是写文件 我最后就直接xmlDoc.Save("test.xml");我知道下次对该文件进行写的时候,如果内存里没有释放这个文件句柄,一定会报错。请问;我应该怎么样使用在每次写完文件,就手动释放这个文件呢?????

解决方案 »

  1.   

    xmlDoc = null;你怎么知道内存没有释放句柄的?
      

  2.   

    我也写了null  又调用了一次,结果报错是另外一个程序正在使用
      

  3.   

    你可以使用using(){}啊!

    “我也写了null 又调用了一次,结果报错是另外一个程序正在使用” 我没遇到过
      

  4.   

    我是这样写的  using(XmlDocument  xmlDoc = new XmlDocument())
                    {
                        xmlDoc.Load(path);
                      .....
                        xmlDoc.Save(path);
                     }
    可是还是报错
    错误 1
    “System.Xml.XmlDocument”: using 语句中使用的类型必须可隐式转换为“System.IDisposable” D:\sendFile\DoUser.cs 43 17
      

  5.   

    加上    // This method call triggers the garbage collector
                //  to collect the unreferenced memory.
                GC.Collect();
                // Wait for the GC's Finalize thread to finish 
                // executing all queued Finalize methods.
                GC.WaitForPendingFinalizers();
    试试好使不好使
      

  6.   

    继承IDisposable接口实现后看看如何