Вопрос

I create a file in isolated storage and then the same file is accessed and be edited on the click of button. But sometime an Exception occured with detail that operation is not allowed on isolated storage. This exception has no fixed time sometimes occured on first click of button some times it occuered after 5th or 6th click etc. ISOFileProcess(String module, String operation, String xmlObj) function in the below code is called on click of button.

public static void ISOFileProcess(String module, String operation, String xmlObj)
    {

        var  store = IsolatedStorageFile.GetUserStoreForApplication();

        if (store.FileExists(IsolatedMemFileHandlingCon.fileName) == false)
        {
            StreamWriter sw = new StreamWriter(store.OpenFile(IsolatedMemFileHandlingCon.fileName, FileMode.OpenOrCreate));
            CreateXML(sw,module,operation,xmlObj);

            sw.Close();
            MessageBox.Show("File Created.");
        }
        else
        {

            //store.OpenFile(IsolatedMemFileHandlingCon.fileName, FileMode.Append);

            IsolatedStorageFileStream isoStream =
                    new IsolatedStorageFileStream(IsolatedMemFileHandlingCon.fileName, FileMode.Append, FileAccess.ReadWrite, store);


            EditXML(isoStream, module, operation, xmlObj);

            isoStream.Close();


        }
    } 

    #endregion

    #region XML Creation And Editing
    private static void CreateXML(StreamWriter sw, String mname, String operation, String xmlObj)
    {

        XmlWriter xWrt = XmlWriter.Create(sw);

        xWrt.WriteStartElement("ocs");
       // xWrt.WriteStartElement("module");

        xWrt.WriteStartElement("operation");

        xWrt.WriteAttributeString("mode", operation);
        xWrt.WriteAttributeString("mname", mname);

        xWrt.WriteRaw(xmlObj);
        xWrt.WriteEndElement();
        //xWrt.WriteEndElement();

        xWrt.WriteEndElement();

        xWrt.Close();
    }

    private static void EditXML(IsolatedStorageFileStream sw, String mname, String operation, String xmlObj)
    {
        sw.Seek(sw.Length - 6, SeekOrigin.Begin);
        XmlWriterSettings wrSettings = new XmlWriterSettings();
        wrSettings.OmitXmlDeclaration = true;


        XmlWriter xWrt = XmlWriter.Create(sw,wrSettings);


        //xWrt.WriteStartElement("module");

        xWrt.WriteStartElement("operation");
        xWrt.WriteAttributeString("mode", operation);
        xWrt.WriteAttributeString("mname", mname);

        xWrt.WriteRaw(xmlObj);

        xWrt.WriteEndElement();
        //xWrt.WriteEndElement();

        xWrt.WriteRaw("</ocs>");

        xWrt.Close();

    }
Это было полезно?

Решение

You specify FileMode.Append here:

 IsolatedStorageFileStream isoStream =
     new IsolatedStorageFileStream(
         IsolatedMemFileHandlingCon.fileName,
         FileMode.Append,
         FileAccess.ReadWrite, store);

Append does not combine with FileAccess.ReadWrite and with moving the cursor before the end of the file.

In the code you use random access, moving the cursor back:

 sw.Seek(sw.Length - 6, SeekOrigin.Begin);

From the MSDN:

Append Opens the file if it exists and seeks to the end of the file, or creates a new file. FileMode.Append can only be used in conjunction with FileAccess.Write. Attempting to seek to a position before the end of the file will throw an IOException and any attempt to read fails and throws an NotSupportedException.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top