当前位置: 代码迷 >> ASP.NET >> 向高手:The process cannot access the file 'X:\X.XXX' because it is being used by another process
  详细解决方案

向高手:The process cannot access the file 'X:\X.XXX' because it is being used by another process

热度:6823   发布时间:2013-02-25 00:00:00.0
向高手求救:The process cannot access the file 'X:\X.XXX' because it is being used by another process
页面外有个Generate按钮 点击了后 调用Generate()方法

但出现的问题是这样的
1.我登陆后 点击Generate按钮 过了6分钟后 被弹出了 要重新登陆 可是Session的时间还没有过 Session是保存在MSSQL中的
2.我再登陆后 点击Generate按钮 差不多有过了6分钟 提示:
The process cannot access the file "C:\\accounts\i-techcomau\products\Products.xml" because it is being used by another process. 

几天了 一直重复的上面的问题

最后说一下 asp 和 aspx 所在的那个文件夹 里面有7万多过个文件了~~~

有什么方法可以解决这个问题啊~~~

using System;
using System.IO;
using System.Data;
using System.Text;
using MYSDatabase.DataSet;
using Microsoft.ApplicationBlocks.Data;

public class FileGenerator
{
// Fields
private int appID = -1;
private string rootFolder = null;
private string websiteName = null;


// Methods
public FileGenerator(int appID, string rootFolder)
{
this.appID = appID;
this.websiteName = new SystemAdmin().GetApplicationWebsite(appID);
this.rootFolder = rootFolder;
}

public string Generate()
{
StreamWriter writer = null;
StreamWriter writer1 = null;
  StreamWriter writer2 = null;
  StreamWriter writer3 = null;
  StreamWriter writer4 = null;
  StreamWriter writer5 = null;
  StreamWriter writer6 = null;
StringBuilder builder = new StringBuilder();
if ((this.appID != -1) && (this.rootFolder != null))
{
  File.Delete(row.RootFolder + @"\" + row.ProductFolder + @"\Products.xml");
  删除的就是C:\\accounts\i-techcomau\products\Products.xml
writer = new StreamWriter(row.RootFolder + @"\" + row.ProductFolder + @"\Products.xml", false);
writer.WriteLine("<?xml version=\"1.0\" ?>");
writer.WriteLine("<products>");
  在这里面开始使用writer~writer6根据数据库生成aspx和asp文件 
}
return builder.ToString();
}
}

------解决方案--------------------------------------------------------
writer.Close()
return builder.ToString(); 

}
------解决方案--------------------------------------------------------
给文件被其他进程使用,也就是说其他进程打开该文件后未关闭。先关闭再进行相关操作。
------解决方案--------------------------------------------------------
writer 使用完成要显示关闭,对文件操作的类是非托管资源,使用完闭后不显示关闭很易容出现这样的Bug
------解决方案--------------------------------------------------------
在使用完非托管资源之后,尽量调用它的Close()或者Dispose()方法关闭,在没有调用这个方法之前别的程序是不能再次操作的。
------解决方案--------------------------------------------------------
探讨
在使用完非托管资源之后,尽量调用它的Close()或者Dispose()方法关闭,在没有调用这个方法之前别的程序是不能再次操作的。
  相关解决方案