Learner
Learner

Reputation: 1542

FileStream for sharing the file between different processes

I am using the following code to modify the XML file that can be used from multiple processes

using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
    var d = XDocument.Load(fs);
    d.Descendants("TestNode").FirstOrDefault().Descendants("MyInfo").FirstOrDefault().Attribute("Id").Value = "Tested Successfully";
    XElement x1 = new XElement(new XElement("PoolId", "A91"));
    d.Root.Add(x1);
    fs.Seek(0, SeekOrigin.Begin);
    d.Save(fs);
}

This method is inside the LOCK object.

Now, I always want to overwrite the content of the XML file so i've used

fs.Seek(0, SeekOrigin.Begin);

is this right? Is there a better way?

Upvotes: 1

Views: 659

Answers (1)

htoverkill
htoverkill

Reputation: 89

Save overwrites already, you don't need to call Seek.

What are you doing with the XElement?

new XElement(new XElement("PoolId", "A91"));

could just read

new XElement("PoolId", "A91");

Upvotes: 1

Related Questions