File used by another process exception

I have two C # .NET applications:

  • One application (say App1) writes data to an XML file.
  • The second application (lets call it App2) reads data from the same XML file and updates.

App2 continually monitors changes to the XML file for which I am using FileSystemWatcher

.

As soon as App1 finishes writing to the file, App2 reads in the changes.

I made sure my App2 reads XML with read-only access, but still sometimes my App1 throws an exception:

"The process cannot access the file" C: \ xmlFile "because it is being used by another process." Here is my code snippet in App2 that reads a file.

Here is my code:

using (var stream = File.Open(filePath,FileMode.Open,FileAccess.Read))
{
    using (XmlReader reader = XmlReader.Create(stream))
    {
        while (reader.Read())
        {
            // Only detect start elements.
            if (reader.IsStartElement())
            {
                switch (reader.Name)
                {
                    case "Component":
                        fileElements.ComponentName = reader["Name"];
                        fileElements.DateRun = reader["DateRun"];
                        fileElements.Passed = reader["Passed"];
                        break;
                }
            }
        }

        if (filePath.ToLower().Contains("ebsserver"))
        {
            UpdateDataTable1(fileElements);
        }
        else if (filePath.ToLower().Contains("ebsui"))
        {
            UpdateDataTable2(fileElements);
        }
        else
        {
            UpdateDataTable3(fileElements);
        }
    }
}

      

How can I fix this?

+3


source to share


2 answers


You should use a FileShare.ReadWrite

reader in your application to signal that there will be no lock when you open. This is the same mechanism that is used, for example, in text editors that open files that are also written to.



File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

      

+1


source


You are missing the FileShare parameter in the File.Open method



    using (var stream = File.Open(filePath,FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (XmlReader reader = XmlReader.Create(stream))
    {
        while (reader.Read())
        {
            // Only detect start elements.
            if (!reader.IsStartElement())
            {
                continue;
            }

            if (reader.Name != "Component")
            {
                continue;
            }

            fileElements.ComponentName  = reader["Name"];
            fileElements.DateRun        = reader["DateRun"];
            fileElements.Passed         = reader["Passed"];
       }

       if (filePath.ToLower().Contains("ebsserver"))
       {
           UpdateDataTable1(fileElements);
       }
       else if (filePath.ToLower().Contains("ebsui"))
       {
           UpdateDataTable2(fileElements);
       }
       else
       {
           UpdateDataTable3(fileElements);
       }
   }

      

0


source







All Articles