簡體   English   中英

該進程無法訪問文件“ filename.xml”,因為它正在被另一個進程使用

[英]The process cannot access the file 'filename.xml' because it is being used by another process

我有一個Web服務項目c#,當我上傳項目@live時出現此錯誤:

System.IO.IOException:該進程無法訪問文件'E:\\ Sites \\ www.bivolino.com \\ bivolino3D \\ bivo \\ imgGal \\ ProductFeedBeslist.xml',因為該文件正在被另一個進程使用。 在System.IO .__ Error.WinIOError(Int32 errorCode,可能是StringFullPath)
在System.IO.FileStream處位於System.IO.FileStream.Init(字符串路徑,FileMode模式,FileAccess訪問,Int32權限,布爾useRights,FileShare共享,Int32 bufferSize,FileOptions選項,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布爾bFromProxy)處。 System.Xml.XmlTextWriter..ctor上的ctor(字符串路徑,FileMode模式,FileAccess訪問權限,FileShare共享)ctor(字符串文件名,編碼編碼)
在e:\\ Sites \\ www.bivolino.com \\ bivolino3D \\ bivo \\ OpenGarments \\ og_webservice \\ App_Code \\ ws_og_bivolinogallery.cs:line 1186中的ws_og_bivolinogallery.GetItemsBeslist()

在我的本地(本地服務器)中,當我運行我的項目時一切正常,但是@live(實時服務器)我的所有方法都不再起作用。 這是我的代碼(以我的新方法為例):

[WebMethod(MessageName = "GetItemsBeslist", Description = "Get a list of GAL shirts", CacheDuration = 3600)]
   public XmlDocument GetItemsBeslist()
   {
   XmlTextWriter textWriter = new XmlTextWriter("E:/Sites/www.bivolino.com/bivolino3D/bivo/imgGal/ProductFeedBeslist.xml", Encoding.UTF8);
   //E:/Sites/www.bivolino.com/bivolino3D/bivo/imgGal
   try
   {
       if (bRegisterIP)
       {
           try { LogFiler.ToLog("### IP ### - [" + sRemoteAddress + "]"); }
           catch { }
       }
       XmlDocument xProducts = new XmlDocument();
       XmlElement subElm;
       XmlElement elmAttr;
       XmlNode elmValue;


       xProducts.CreateXmlDeclaration("1.0", "utf-8", null);
       XmlElement topElm = xProducts.CreateElement("ProductFeed");
       topElm.SetAttribute("version", "1.0");
       topElm.SetAttribute("timestamp", System.DateTime.Now.ToString().Replace(" ", ":"));
       xProducts.AppendChild(topElm);

       List<string[]> strarrVelden = new List<string[]>();
       strarrVelden.AddRange(DB.GetItemsBeslist());
       foreach (string[] rij in strarrVelden)
       {

           subElm = xProducts.CreateElement("Product");

           elmAttr = xProducts.CreateElement("ProductTitle");
           elmValue = xProducts.CreateNode(XmlNodeType.Text, "ProductTitle", null); elmValue.Value = "Herenoverhemd Bivolino " + rij[5].ToString();
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);

           elmAttr = xProducts.CreateElement("Price");
           elmValue = xProducts.CreateNode(XmlNodeType.Text, "Price", null); elmValue.Value = rij[6].ToString().Replace(",", ".");
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);


           elmAttr = xProducts.CreateElement("productURL");
           elmValue = xProducts.CreateNode(XmlNodeType.CDATA, "productURL", null); elmValue.Value = rij[1].ToString();
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);


           elmAttr = xProducts.CreateElement("Category");
           elmValue = xProducts.CreateNode(XmlNodeType.Text, "Category", null); elmValue.Value = "Herenoverhemd ";
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);


           elmAttr = xProducts.CreateElement("ProductDescription");
           elmValue = xProducts.CreateNode(XmlNodeType.Text, "ProductDescription", null); elmValue.Value = rij[2].ToString();
           elmAttr.AppendChild(elmValue);
           subElm.AppendChild(elmAttr);



           topElm.AppendChild(subElm);
       }

       textWriter.WriteStartDocument();

       xProducts.Save(textWriter);
       textWriter.WriteEndDocument();
       textWriter.Close();

       return xProducts;


   }
   catch (Exception ex)
   {
       return ErrHandle("ERROR - GetItemsBeslist - " + ex.Message, "ERROR - GetItemsBeslist");
   }

}

通常這些錯誤來自未關閉的文件流,但是我已經做好了。 我想我已經忘記了重要的一步,但無法弄清楚在哪里。 非常感謝您的幫助

我只會在方法的末尾創建XmlTextWriter (實際上需要時),並且還要使用using塊。 using (var textWriter = new XmlTextWriter ("")) { ... }而且,可以同時由不同線程調用此方法嗎? 如果是這樣,則必須處理並發。

嘗試使用以下方法包裝流ctor:

using (XmlTextWriter textWriter = new XmlTextWriter("E:/Sites/www.bivolino.com/bivolino3D/bivo/imgGal/ProductFeedBeslist.xml", Encoding.UTF8)){

...
}

您的代碼似乎並不會在發生異常時關閉流,請注意using 即使XmlTextWriter是IDisposable的,它也可能要花一些時間才能處理掉,但在此之前您可能已經在使用方法了。

另外-了解序列化,沒有必要像您一樣手動創建XML。

考慮使用Server.MapPathHostingEnvironment來獲取文件系統鏈接。 using語句應解決該問題。

[WebMethod(MessageName = "GetItemsBeslist", Description = "Get a list of GAL shirts", CacheDuration = 3600)]
public XmlDocument GetItemsBeslist()
{
    if (bRegisterIP)
    {
       try { LogFiler.ToLog("### IP ### - [" + sRemoteAddress + "]"); }
       catch { }
    }

    try
    {
       var xProducts = GetProducts();
       string file = Server.MapPath("/bivolino3D/bivo/imgGal/ProductFeedBeslist.xml");

       using(XmlTextWriter textWriter = new XmlTextWriter(file, Encoding.UTF8))
       {
           textWriter.WriteStartDocument();
           xProducts.Save(textWriter);
           textWriter.WriteEndDocument();
       }

       return xProducts;
    }
    catch (Exception ex)
    {
       return ErrHandle("ERROR - GetItemsBeslist - " + ex.Message, "ERROR - GetItemsBeslist");
    }
}

private XmlDocument GetProducts()
{
    XmlDocument xProducts = new XmlDocument();
    XmlElement subElm;
    XmlElement elmAttr;
    XmlNode elmValue;


    xProducts.CreateXmlDeclaration("1.0", "utf-8", null);
    XmlElement topElm = xProducts.CreateElement("ProductFeed");
    topElm.SetAttribute("version", "1.0");
    topElm.SetAttribute("timestamp", System.DateTime.Now.ToString().Replace(" ", ":"));
    xProducts.AppendChild(topElm);

    List<string[]> strarrVelden = new List<string[]>();
    strarrVelden.AddRange(DB.GetItemsBeslist());
    foreach (string[] rij in strarrVelden)
    {

       subElm = xProducts.CreateElement("Product");

       elmAttr = xProducts.CreateElement("ProductTitle");
       elmValue = xProducts.CreateNode(XmlNodeType.Text, "ProductTitle", null); elmValue.Value = "Herenoverhemd Bivolino " + rij[5].ToString();
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);

       elmAttr = xProducts.CreateElement("Price");
       elmValue = xProducts.CreateNode(XmlNodeType.Text, "Price", null); elmValue.Value = rij[6].ToString().Replace(",", ".");
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);


       elmAttr = xProducts.CreateElement("productURL");
       elmValue = xProducts.CreateNode(XmlNodeType.CDATA, "productURL", null); elmValue.Value = rij[1].ToString();
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);


       elmAttr = xProducts.CreateElement("Category");
       elmValue = xProducts.CreateNode(XmlNodeType.Text, "Category", null); elmValue.Value = "Herenoverhemd ";
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);


       elmAttr = xProducts.CreateElement("ProductDescription");
       elmValue = xProducts.CreateNode(XmlNodeType.Text, "ProductDescription", null); elmValue.Value = rij[2].ToString();
       elmAttr.AppendChild(elmValue);
       subElm.AppendChild(elmAttr);



       topElm.AppendChild(subElm);
    }

    return xProducts;
}

我還將在textWriter.Close()之前或在using塊的最后一行調用textWriter.Flush()。 有時.NET在關閉時進行IO操作時需要花費一些時間從緩沖區寫入基礎流,這可能會鎖定文件。

您可以在確實需要時嘗試打開該文件,並序列化對多個線程的訪問。

與其在方法開始時打開文本編寫器,不如在方法需要的地方打開它,即在方法結束時還定義了靜態全局對象objLock,您可以在該對象上獲取鎖。 這應該工作

 private static  object objLock = new object();

 lock(objLock)
 {
   using (XmlTextWriter textWriter = new    XmlTextWriter("E:/Sites/www.bivolino.com/bivolino3D/bivo/imgGal/ProductFeedBeslist.xml", Encoding.UTF8))
    {
       textWriter.WriteStartDocument();
       xProducts.Save(textWriter);
       textWriter.WriteEndDocument();
       textWriter.Close();


     }

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM