Skip to content

Commit

Permalink
feature: handle file lock issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Bolorunduro Winner-Timothy committed Nov 27, 2017
1 parent d7170ef commit 732fe20
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
1 change: 0 additions & 1 deletion PropertyConfig.Tests/PropertyConfig.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="4.19.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="NUnit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
Expand Down
17 changes: 11 additions & 6 deletions PropertyConfig/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public void LoadFromXml(string filePath)
throw new FileNotFoundException("The given file doesn't exist.");
}
XmlDocument xmlDocument = new XmlDocument();
var stream = new FileStream(filePath, FileMode.Open);
xmlDocument.Load(stream);
if (xmlDocument.DocumentElement == null) return;
foreach (XmlNode node in xmlDocument.DocumentElement.ChildNodes[0])
using (var stream = new FileStream(filePath, FileMode.Open))
{
this[node.Name] = node.InnerText;
xmlDocument.Load(stream);
if (xmlDocument.DocumentElement == null) return;
foreach (XmlNode node in xmlDocument.DocumentElement.ChildNodes[0])
{
this[node.Name] = node.InnerText;
}
}
}

Expand Down Expand Up @@ -87,7 +89,10 @@ public void StoreToXml(string filePath, string comment)
{
File.Delete(filePath);
}
xmlDocument.Save(File.OpenWrite(filePath));
using (var stream = File.OpenWrite(filePath))
{
xmlDocument.Save(stream);
}
}

/// <summary>
Expand Down

0 comments on commit 732fe20

Please sign in to comment.