Click here to Skip to main content
16,018,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 C# .net applications, One application (lets say App1) writes data to xml file and the second application (lets call it App2) reads data from the same xml file and updates itself. The App2 continuously monitors for the changes in xml file, for which I am using FileSystemWatcher. As soon as the App1 completes writing to the file, the App2 reads the changes. I made sure my App2 is reading the xml only with Read access but still sometime my App1 throws an exception with
"The process cannot access the file 'C:\xmlFile' because it is being used by another process". Here is my code snippet in App2 which is reading the file.

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);
                   }
               }
Posted

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900