一个XML文件life.xml在一个前台页面中被使用,后台程序如下:
private void Page_Load(object sender, System.EventArgs e)
{
a......
DataTable dt;
DataSet ds;
string xmlPath  = AppDomain.CurrentDomain.BaseDirectory + "life.xml";
ds = new DataSet();
ds.ReadXml(xmlPath);
dt = ds.Tables[YLWriteDataRule.MovieCommend];
dl.DataBind();......
}然后另外一个后台程序对life.xml定时进行更新,程序如下,该程序和上面的程序在同一WEB中:[WebMethod]
public string CopyFile()
{
string path = HttpContext.Current.Request.PhysicalApplicationPath;
try
{
sourefile = Server.MapPath(".") + "\\temp.xml"; //temp.xml文件的内容会定时更新
destfile = Server.MapPath(".") + "\\life.xml";
File.Copy(sourefile , destfiel , true);
return "fiel copy succeeded。";
}
catch(Exception ex)
{
return ex.Message;
}
}现在的问题是,当执行File.Copy(sourefile , destfiel , true)时,说文件life.xml没有访问权限。但是如果我进行下面2中测试,却可以拷贝,第1种测试案例是:
destfile = Server.MapPath(".") + "\\life_111.xml";
File.Copy(sourefile , destfiel , true);
这样可以执行成功,原因是不是life.xml在前台网页中有很多用户在不断调用,而life_111.xml是没有人使用的。
是这样吗?第1种测试案例是,我写了个控制台程序,如下,其中d:\webpub\是网站的发布路径:
using System;
using System.IO;
namespace fc
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string path = "d:\\webpub\\temp.xml";
string path2 = "d:\\webpub\\life.xml";
try 
{
File.Copy(path, path2, true);
Console.WriteLine("The  Copy operation succeeded, which was expected.");

catch 
{
Console.WriteLine("The copy is not allowed, which was not expected.");
}
}
}
}
在这种方式下,可以把temp.xml拷贝到life.xml。所以总结一下,现在情况是:
在WEB程序中,因为life.xml被前面页面在调用,所有不能把temp.xml拷贝到life.xml中,但可以把temp.xml拷贝到没有被前台页面使用的life_111.xml文件中。但是如果不在WEB程序中,直接写一个控制台程序,却可以把temp.xml拷贝到life.xml中,虽然这是life.xml依然在被网站页面使用。需要解决的问题是:怎么在在WEB程序中,能把temp.xml更新到life.xml中。